我有一个简单的 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
返回undefined
,这不是一个函数,也是错误的原因
即setInterval 是一个函数,但它返回的不是函数 - 只需删除 , 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 ()
}
关于javascript - 在 java 脚本中的 setTimeout 方法内调用 setInterval 函数获取 setInterval(...) 不是函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58245063/