Run cell only once / Static timestamp

Hi folks,

After some quick search I guess this might be one necessity in the notebooks. I wanted to create one static timestamp in my markdown part however the new Date() syntax will update every time the notebook get reloaded, and I wonder there’s one tricky way to have a static timestamp.
It also means that let certain cell only run once and suppress updating the cell even though the notebook get reloaded.
I’ve tried once = require('') but I didn’t manage to get it work, also there’s a jquery one method but unfortunately I have no clue how to use it in markdown tagged literals.
Thanks for any sort of input you could provide!

I think you’d need to manually input the timestamp, as Observable will always run all the cells when the notebook is reloaded.

Thank you so much, it didn’t come to my mind such an easy trick!

You want a timestamp from the first time each user visits your notebook, which then persists when they reload?

You could try storing the timestamp in browser localStorage.

Good idea, then I tried this:

but when I reload the page the localStorage'd item got updated again, any idea how to fix it?

Oh, I get it, the trick is to claim an object that stores the information.

Oh, the timestamp is still updating itself every time I reload the page…


timestamp = new Date(
  +localStorage.timestamp ||
  +(localStorage.timestamp = +new Date))

I’m just using localStorage as if it’s a regular object; you might prefer to use the getItem / setItem methods, along the lines of:

timestamp = new Date(
  +localStorage.getItem('timestamp') ||
  +(localStorage.setItem('timestamp', +new Date), localStorage.getItem('timestamp')))
1 Like