8

I was just wondering if it is possible to have a javascript for loop that only iterates through the loop once a day i.e. when the date changes?

for(i=0; i < myArray.length; i++){

    alert(myArray[i]);

}

So in the above loop, let it run, and freeze it or something only till the data changes, and the do another iteration, and just keep on doing that.. You know what I mean.

Thanks in advance!

vsync
  • 118,978
  • 58
  • 307
  • 400

3 Answers3

18

Using localStorage is the best way to go when you don't have a server (because a user can change the computer's time and break your logic, and using a server it's harder to hack this)

Method below is more bulletproof:

// checks if one day has passed. 
function hasOneDayPassed()
  // get today's date. eg: "7/37/2007"
  var date = new Date().toLocaleDateString();

  // if there's a date in localstorage and it's equal to the above: 
  // inferring a day has yet to pass since both dates are equal.
  if( localStorage.yourapp_date == date ) 
      return false;

  // this portion of logic occurs when a day has passed
  localStorage.yourapp_date = date;
  return true;
}


// some function which should run once a day
function runOncePerDay(){
  if( !hasOneDayPassed() ) return false;

  // your code below
  alert('Good morning!');
}


runOncePerDay(); // run the code
runOncePerDay(); // does not run the code
vsync
  • 118,978
  • 58
  • 307
  • 400
  • at the bottom you have written `runOncePerDay(); //run the code` `runOncePerDay(); //does not run the code` but they are identical? What am I missing? – jmfbot Oct 02 '18 at 11:48
  • 1
    @jmfbot - it's just a demonstration that shows the function can only run once a day, so the second call won't *alert* anything since on the second call `hasOneDayPassed()` will return true (until 1 day passses) – vsync Oct 02 '18 at 17:46
4

If you want something to happen at predefined intervals, you can set a timeout/interval: http://www.w3schools.com/js/js_timing.asp

For example:

var dayInMilliseconds = 1000 * 60 * 60 * 24;
setInterval(function() { alert("foo"); },dayInMilliseconds );

edit: since you mentioned that the code will be running in a browser, this assumes the browser is running for at least 24 hrs and will not work otherwise.

nolegs
  • 608
  • 4
  • 11
  • 1
    This is basically right, although I think the OP wants it to run local-time midnight. If that's the case, you should instead compute `millisecondsToMidnight`. – apsillers Jul 31 '12 at 14:10
  • @nolegs That's really good, thanks nolegs. But as apsillers says, I wouldn't like it to run every 24hours from time of execution. But itterate every night at midnight. –  Jul 31 '12 at 14:32
  • You can see here: http://stackoverflow.com/questions/8583694/determine-minutes-until-midnight This will give you milliseconds until midnight, but the browser will obviously still have to be running at midnight for this to work. – nolegs Jul 31 '12 at 14:37
3

the best way to achieve it is by creating a cookie that lasts for1 day.. Even if after the refresh of the web page or browser gets closed that countdown will still continue..

setcookie($cookie_name, $cookie_value, time() + 86400, "/"); 

This means 86400 = 1 day

Hope it helps

Evangelos
  • 69
  • 9