使用 clearInterval() 和 setInterval() 重置后未调用的函数

问题描述 投票:0回答:1

下面的代码最初设置了1000ms的间隔。这行得通(除其他外,它设置了当前时间)——每一秒都过长,但客户希望时间准确。所以;我写了下面的函数,一旦秒数为零,就将间隔重置为 60 秒(所以分钟会在分钟内更新)

var UpdateBlocks=[];
        
function updateBlock(n) {
    console.log("In updateBlock("+n.toString()+")");
    
    if( UpdateBlocks.length > n && UpdateBlocks[n] !== null ) {
        let TimeStr = new Date().toLocaleString("en-US", { timeZone: "America/Los_Angeles" });
        let today = new Date(TimeStr);
        let seconds = today.getSeconds();
        if( seconds == 0 ) { // we're on the minute now..
            // reset to 1 minute upadte.
            clearInterval(UpdateBlocks[n]);
            console.log("Setting 60 second interval: " + "'updateBlock(" + n.toString() + ")'");
            var nhtmp = setInterval("'updateBlock(" + n.toString() + ");'", 60000);
            console.log("New handle: "+nhtmp.toString()); // Is a new handle #
            UpdateBlocks[n] = null; 
        }
    }
    
    console.log("Calling setBlockValues(" + n.toString() + ")");
    setBlockValues(n);
}

控制台记录每个(1 秒)调用。 它记录它正在重置间隔。 但是这个函数再也不会被调用了。

php setinterval clearinterval
1个回答
0
投票

我将 " 和 ' 混合到同一个字符串中——这就是原因。菜鸟错误。

© www.soinside.com 2019 - 2024. All rights reserved.