每天运行一次代码 [英] Run code once a day

查看:100
本文介绍了每天运行一次代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否有可能有一个javascript for循环,该循环仅每天一次(即日期更改时)循环遍历该循环?

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.

提前谢谢!

推荐答案

使用

Using localStorage is the best way to go when you don't have a server, since the javascript code might restarted (by closing the tab and re-opening), therefore loosing the previous state.

// 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

这篇关于每天运行一次代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆