我只是想知道是否有可能有一个 javascript for 循环,每天只迭代一次循环,即当日期更改时?
for(i=0; i < myArray.length; i++){
alert(myArray[i]);
}
所以在上面的循环中,让它运行,然后卡住它或其他东西,直到数据发生变化,然后再进行一次迭代,然后继续这样做。你知道我的意思。
提前致谢!
最佳答案
使用 localStorage当您没有服务器时,这是最好的方法(因为用户可以更改计算机的时间并破坏您的逻辑,并且使用服务器更难破解它)
下面的方法更可靠:
// 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
关于javascript - 每天运行一次代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11741979/