jQuery 如何在特定时间和日期运行函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19088040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a function at specific time & date?
提问by Shannon Hochkins
How can I run a function at a given time and date?
如何在给定的时间和日期运行函数?
Example: I have a function that needs to run on the 12th of each month at 10AM.
示例:我有一个需要在每月 12 日上午 10 点运行的函数。
This page will be running 24/7, if this is important.
如果这很重要,此页面将 24/7 全天候运行。
Obviously I'd have to compare against the current date, but I'm not sure how to check if the current date and time has been matched.
显然我必须与当前日期进行比较,但我不确定如何检查当前日期和时间是否匹配。
Shannon
香农
回答by Alnitak
It's not advised to use setInterval
because it has non-deterministic behaviour - events can be missed, or fire all at once. Time will fall out of sync, too.
不建议使用setInterval
它,因为它具有非确定性行为 - 事件可能会被错过,或同时触发。时间也会不同步。
The code below instead uses setTimeout
with a one minuteperiod, where each minute the timer is resynchronised so as to fall as closely to the hh:mm:00.000s point as possible.
下面的代码,而不是使用setTimeout
与1分钟毫米:00.000s点尽可能周期,其中该定时器被重新同步,从而每分钟到尽可能接近下降到HH。
function surprise(cb) {
(function loop() {
var now = new Date();
if (now.getDate() === 12 && now.getHours() === 12 && now.getMinutes() === 0) {
cb();
}
now = new Date(); // allow for time passing
var delay = 60000 - (now % 60000); // exact ms to next minute interval
setTimeout(loop, delay);
})();
}
回答by flup
You can instantiate two Date objects. One for now and one for the next instance of the event. Now is easy: new Date(). For the next instance you can loop through the options till you find one larger than now. Or do some more sophisticated date time wizardry. Compare the getTime() of the both, and then do a setTimeout for the alert.
您可以实例化两个 Date 对象。一个用于现在,一个用于事件的下一个实例。现在很简单:new Date()。对于下一个实例,您可以循环选择选项,直到找到比现在更大的选项。或者做一些更复杂的日期时间魔法。比较两者的 getTime(),然后对警报执行 setTimeout。
EDIT: Updated since @Alnitak points out that there's a maximum to the timeout, see setTimeout fires immediately if the delay more than 2147483648 milliseconds.
编辑:更新,因为@Alnitak 指出超时有最大值,如果延迟超过 2147483648 毫秒,请参阅setTimeout 立即触发。
function scheduleMessage() {
var today=new Date()
//compute the date you wish to show the message
var christmas=new Date(today.getFullYear(), 11, 25)
if (today.getMonth()==11 && today.getDate()>25)
christmas.setFullYear(christmas.getFullYear()+1)
var timeout = christmas.getTime()-today.getTime();
if( timeout > 2147483647 ){
window.setTimeout( scheduleMessage(), 2147483647 )
} else {
window.setTimeout(function() {alert('Ho Ho Ho!'); scheduleMessage()}, timeout)
}
}
回答by bhb
On the page where o want to do the check add this
在要进行检查的页面上添加此
setInterval(function () {
var date = new Date();
if (date.getDate() === 12 && date.getHours() === 10 && date.getMinutes === 0) {
alert("Surprise!!")
}
}, 1000)
Update- add date.getSeconds == 0
to limit it to fire only one at 10:00:00. Thanks to comments below
更新 - 添加date.getSeconds == 0
以限制它在 10:00:00 仅触发一次。感谢下面的评论
回答by Kasper Franz
You can use something like this
你可以使用这样的东西
var runned = false;
var d = new Date();
if(d.getDate() == 12 && d.getHours() == 10 && !runned){
//Do some magic
runned = true;
}
If you want some with the minute (and not the whole hour you can add d.getMinutes()
如果你想要一些分钟(而不是整个小时,你可以添加 d.getMinutes()
回答by gwillie
maybe use and iframe
with meta refresh and workout content server side
也许使用和iframe
元刷新和锻炼内容服务器端
<meta http-equiv="refresh" content="{CHANGE_THIS_TO_WHAT_YOU_CALCULATED_AT_SERVER}">
or use javascripts setInterval
或使用 javascripts setInterval
var interval = 300000; // run in 5 minutes
window.setInterval("reloadRefresh();", interval);
function reloadRefresh() {
// do whatever
}
and
和