是否可以使用javascript(jQuery解决方案也可以)在一天的特定时间触发事件/调用某个功能,例如: 10:00在一天中的某个特定时间发生火灾事件
呼叫myFunctionA
14:00 等呼叫myFunctionB ..
感谢
马克
是否可以使用javascript(jQuery解决方案也可以)在一天的特定时间触发事件/调用某个功能,例如: 10:00在一天中的某个特定时间发生火灾事件
呼叫myFunctionA
14:00 等呼叫myFunctionB ..
感谢
马克
4html:
current date: <span id="cd"></span><br />
time to Alarm: <span id="al1"></span><br />
alarm Triggered: <span id="al1stat"> false</span><br />
的javascript:
var setAlarm1 = "14"; //14:00, 2:00 PM
var int1 = setInterval(function(){
var currentHour = new Date().getHours();
var currentMin = new Date().getMinutes();
$('#cd').html(currentHour + ":" + currentMin);
$('#al1').html(setAlarm1 - currentHour + " hours");
if(currentHour >= setAlarm1){
$('#al1stat').html(" true");
clearInterval(int1);
//call to function to trigger : triggerFunction();
}
},1000) //check time on 1s
这是一个糟糕的解决方案。你正在浪费无用的测试(每秒!?!)。只需计算时差(您已经做到了),并设置差异的时间间隔。 – xryl669
/**
* This Method executes a function certain time of the day
* @param {type} time of execution in ms
* @param {type} func function to execute
* @returns {Boolean} true if the time is valid false if not
*/
function executeAt(time, func){
var currentTime = new Date().getTime();
if(currentTime>time){
console.error("Time is in the Past");
return false;
}
setTimeout(func, time-currentTime);
return true;
}
$(document).ready(function() {
executeAt(new Date().setTime(new Date().getTime()+2000), function(){alert("IT WORKS");});
});
完美!谢谢 – moeiscool
难道情况下自动发射,或者仅在特定的时间点? – reporter
Duplicate:http://stackoverflow.com/questions/4455282/call-a-javascript-function-at-a-specific-time-of-day –