Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript setTimeout是否继续在其中运行函数?_Javascript_Timer - Fatal编程技术网

Javascript setTimeout是否继续在其中运行函数?

Javascript setTimeout是否继续在其中运行函数?,javascript,timer,Javascript,Timer,因此,我有一个从onload事件调用的函数: var called = 0; function getAll() { var msg = "hello everyone"; alert("hi"); setTimeout(startTimerRepeat, 5000, msg); } function startTimerRepeat(content) { document.getElementById('howmanyLbl').innerText = "Calle

因此,我有一个从
onload
事件调用的函数:

var called = 0;

function getAll()
{
   var msg = "hello everyone";
   alert("hi");
   setTimeout(startTimerRepeat, 5000, msg);
}

function startTimerRepeat(content)
{
   document.getElementById('howmanyLbl').innerText = "Called " + called + " times, " + msg;
}
我希望每隔5秒调用一次
startTimerRepeat()
函数,它确实如此。然而,
getAll()
函数似乎也每5秒执行一次,因为每隔5秒会连续出现一个带有“hi”的警报

有人知道我做错了什么吗

但是,getAll()函数似乎也每5分钟执行一次 秒,每5秒持续出现一个带有“hi”的警报

如果您不希望每次都收到
hi
警报,但仍然希望每5秒更新一次innerText,请将方法更改为

var called = 0;
function getAll()
{
   var msg = "hello everyone";
   alert("hi");
   setTimeout(function(){
      startTimerRepeat(msg );
   }, 5000, msg);
}

function startTimerRepeat(msg)
{
   document.getElementById('howmanyLbl').innerText = "Called " + (called++) + " times, " + msg;
   setTimeout(function(){
      startTimerRepeat(msg );
   }, 5000, msg);
}

我希望startTimerRepeat()函数每5秒调用一次
您在哪里执行?谁在每隔5秒呼叫一次
startTimerRepeat
?回答标题中的问题:不,
setInterval
继续。您需要为您的问题添加更多代码,以便我们了解这一点。特别是从中调用
getAll()
的位置。另外,
setTimeout()
仅在延迟后执行一次函数
setInterval()
是您在这里寻找的。我假设
startTimerRepeat()
仅每5秒被调用一次,因为您在其他地方每隔5秒调用
getAll()
,因为
setTimeout()
只执行一次函数,在指定的延迟之后。这无法回答问题,因为OP没有提供导致错误的所有代码-即导致每5秒调用
getAll
的代码