在 java 脚本获取 setInterval(...) 的 setTimeout 方法内调用 setInterval 函数不是函数错误

Calling setInterval function inside a setTimeout method in java-script getting setInterval(...) is not a function error

我有一个简单的 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)()

...因为setIntervalreturnsundefined,这不是函数,也是错误的原因

即setInterval 是一个函数,但它 returns 不是 - 只需删除 , 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 ()
}