运行 在特定的 UTC +5:30 时间上运行一次
Run function on a specific UTC +5:30 Time once
我有一个函数 main()。
我想在 9:25、1:25、5:25 的时间 运行 这个功能(考虑到 12 小时制时间 - 意味着 9:30 在早上和晚上) .
当时钟到达上述任何时间时,函数应该 运行 一次。然后函数应该等待下一次。
就像早上 9:30 的功能 运行s(但只有一次然后停止)然后它应该在下午等待 1:25。我希望所有这些都编码在 Javascript 中。有没有什么调度程序可以帮助我...
做这样的事情:
var times = [[9, 25], [1, 25], [5, 25]];
var lastrun;
function checktime(){
now = new Date(); // Get the current time
if (lastrun != null){
if (now-lastrun > 60000) return; // Wait until a minute has elapsed
// (In order to avoid the function running twice within the same minute)
}
ok = false;
for (time of times){ // Iterate through target times
target = new Date(); // Create a new date where the target time will be held
target.setHours(time[0]); // Set the hour and minute to the target time
target.setMinutes(time[1]);
if (now.getTime() >= target.getTime()){ // Check if the target time has been reached
ok = true;
break;
}
}
if (ok){
lastrun = now; // Store the time when the function was last run
main(); // Run the main function if 'ok' is true
}
}
setInterval(checktime, 1000);
我有一个函数 main()。 我想在 9:25、1:25、5:25 的时间 运行 这个功能(考虑到 12 小时制时间 - 意味着 9:30 在早上和晚上) . 当时钟到达上述任何时间时,函数应该 运行 一次。然后函数应该等待下一次。 就像早上 9:30 的功能 运行s(但只有一次然后停止)然后它应该在下午等待 1:25。我希望所有这些都编码在 Javascript 中。有没有什么调度程序可以帮助我...
做这样的事情:
var times = [[9, 25], [1, 25], [5, 25]];
var lastrun;
function checktime(){
now = new Date(); // Get the current time
if (lastrun != null){
if (now-lastrun > 60000) return; // Wait until a minute has elapsed
// (In order to avoid the function running twice within the same minute)
}
ok = false;
for (time of times){ // Iterate through target times
target = new Date(); // Create a new date where the target time will be held
target.setHours(time[0]); // Set the hour and minute to the target time
target.setMinutes(time[1]);
if (now.getTime() >= target.getTime()){ // Check if the target time has been reached
ok = true;
break;
}
}
if (ok){
lastrun = now; // Store the time when the function was last run
main(); // Run the main function if 'ok' is true
}
}
setInterval(checktime, 1000);