我有一个简单的setTimeout函数,该函数在特定时间运行并且运行良好:
var now = new Date();
var milliTillExec = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), 0, 0) - now;
if (milliTillExec < 59500) {
milliTillExec += 59500;
}
window.setTimeout(function(){
console.log('at 59:500');
},milliTillExec);
试图添加一个功能,该功能在上一个功能触发后每300毫秒运行一次,所以我这样做是这样的:
function runEvery300Milli(){
var t = new Date();
window.setInterval(function(){
if((t.getMinutes===59 && t.getMilliseconds>499)||(t.getMinutes===0 && t.getMilliseconds<500)){
console.log(t.getMinutes()+ ":"+t.getSeconds() + ":"+ t.getMilliseconds());
}
}, 300)();
}
var now = new Date();
var milliTillExec = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), 0, 0) - now;
if (milliTillExec < 59500) {
milliTillExec += 59500;
}
window.setTimeout(function(){
console.log('at 59:500');
runEvery300Milli();
},milliTillExec);
但出现以下错误:
Uncaught TypeError: setInterval(...) is not a function
at runEvery300Milli
您的setinterval看起来像
setInterval(fn, 300)()
...由于setInterval
return undefined
,这不是函数,而是错误的原因
即setInterval是一个函数,但它返回的不是-只需删除()
after, 300)
function runEvery300Milli(){
var t = new Date();
window.setInterval(function(){
if((t.getMinutes===59 && t.getMilliseconds>499)||(t.getMinutes===0 && t.getMilliseconds<500)){
console.log(t.getMinutes()+ ":"+t.getSeconds() + ":"+ t.getMilliseconds());
}
}, 300); // <=== removed trailing ()
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句