我需要创建一种特殊的脚本。
我想在一天中的特定时间显示一条消息。我已经在 Firebug 控制台中测试了代码并且它可以工作。代码是:
//Getting the hour minute and seconds of current time
var nowHours = new Date().getHours() + '';
var nowMinutes = new Date().getMinutes() + '';
var nowSeconds = new Date().getSeconds() + '';
var this_event = nowHours + nowMinutes + nowSeconds;
//172735 = 4PM 25 Minutes 30 Seconds. Just checked if now is the time
if (this_event == "162530") {
window.alert("Its Time!");
}
我感觉Script不是每秒钟都在运行。为了使其有效工作,脚本必须能够检查时分和秒“每秒”。我不担心性能,我只需要准确计时(精确到秒)。
我该怎么做?
最佳答案
当然,脚本不是每秒运行一次,GM 脚本在文档加载后运行一次。
计算当前时间和目标时间之间的差异,并根据差异使用超时:
var now=new Date(),
then=new Date(),
diff;
then.setHours(16);
then.setMinutes(15);
then.setSeconds(30);
diff=then.getTime()-now.getTime();
//when time already has been reached
if(diff<=0){
window.alert('you\'re late');
}
//start a timer
else{
window.setTimeout(function(){window.alert('it\'s time');},diff);
}
关于javascript - 创建预定的 Greasemonkey 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23604458/