0

When someone visits my site, a certain javascript function must be called. But when they refresh the page, or go to another page, this function should NOT be executed again for at least 24 hours.

Even when the web browser is closed and reopened, the function should not be executed, until it's 24 hours later.

So I was thinking of a Javascript or jQuery function that writes a cookie with a time stamp value when the function is executed.

But before the function is executed, it should check if there is a cookie.

  • If it's NOT there, execute the function.
  • If it's there, check the time value in the cookie and compare it with the current time.
  • Is the time difference 24 hours or more, then execute the function. (and overwrite the cookie time value, for the next 24 hours)
  • Is the time difference less than 24 hours, do NOT execute the function.

Would this be the right approach to achieve the goal? Or is there a built in function that does this? And could someone please help me create some javascript or jQuery code to achieve this?

Thank you.

EDIT: I found the following code here: Run code once a day:

<script type='text/javascript'>
function hasOneDayPassed() {
    var date = new Date().toLocaleDateString();
    if( localStorage.yourapp_date == date ) return false;
    localStorage.yourapp_date = date;
    return true;
}

function runOncePerDay() {
    if( !hasOneDayPassed() ) return false;
    alert("blabla");
}

runOncePerDay();

</script>

I tried it. The date value IS stored in local storage. But somehow the function (alert for testing purposes) is still executed when closing and reopening the web browser (while the local storage value is still there)

Maurice
  • 87
  • 8
  • 3
    Your strategy sounds about right. Give it a try: show us what you’ve done and where you got stuck. – Terry Jul 11 '20 at 23:54
  • 1
    This sounds perfect. You can try this one and achieve this goal easily. When you are `stuck` somewhere in your `code` then paste your example code and someone will be happy to help you. Good luck. – Always Helping Jul 11 '20 at 23:55
  • I found and tested some code. But it does not work yet... The value IS stored in local storage. But somehow the function is still executed when closing and reopening the web browser – Maurice Jul 12 '20 at 00:22
  • @Maurice, your does run every time because you are not setting yourapp_date when you run runOncePerDay! – Ashwin Jul 12 '20 at 00:24
  • try setting it with localStorage.setItem("yourapp_date", new Date()) in your runOncePerDay function. Happy coding! – Ashwin Jul 12 '20 at 00:25
  • The problem is that after closing/opening the web browser the localStorage value returns NULL. But when I look in the web browsers local storage, I clearly see that the value IS there. I even tried a delay, but still it only returns the value when the browser is NOT restarted. And when the value is null, the comparison fails and the function is executed. So why is the value null after restarting the browser (even though it is stored in the browser) – Maurice Jul 12 '20 at 01:29

2 Answers2

2

using cookies or local storage would work, but someone could easily just clear their cookies/local storage or edit the client side code. So if it's really important that they can't re-run the function then you might want to look into storing this information on the server

Ben Baldwin
  • 427
  • 1
  • 6
  • 13
1

You might consider doing something like:

function oncePerDay(func){
  let dt = new Date, tm = dt.getTime();
  if(localStorage.timestamp){
    if(tm-localStorage.timestamp > 86399999){
      localStorage.timestamp = tm; func(tm);
    }
  }
  else{
    localStorage.timestamp = tm; func(tm);
  }
}
addEventListener('load', ()=>{
  oncePerDay(tm=>{
    console.log(tm);
  });
});

Of course, if the Client is on the Browser for over 24 hours this won't work without an interval to check for the time change.

StackSlave
  • 10,613
  • 2
  • 18
  • 35
  • This does work, however it does NOT work when the browser was closed and reopened. Just like the example code above, for some reason after reopening the browser, localStorage.timestamp returns null. When I look in the browser's localstorage, I see that the time value IS there. – Maurice Jul 12 '20 at 10:54
  • 1
    The time is there, as a String in `localStorage.timestamp`, because that is the number that I'm subtracting from the `dateInstance.getTime()`. The function runs once every 24 hours. That's what you asked for. – StackSlave Jul 13 '20 at 20:59