简体   繁体   English

如何循环 setTimeout() 以及如何停止循环

[英]How to loop setTimeout() and how to stop the loop

I have a setTimeout() looped which works just fine except that I don't know how to stop when I need it stop it.我有一个 setTimeout() 循环,它工作得很好,只是我不知道如何在需要时停止它。

I have read a few posts with similar inquiries but for some reason I cannot make those solutions fit my case and here's hoping someone can help me out.我已经阅读了一些类似查询的帖子,但由于某种原因,我无法使这些解决方案适合我的情况,希望有人能帮助我。

var timeout_function;
function testFunction(param1, param2, param3, action){
    timeout_function = setTimeout(function start() {
        if(action == 'start'){
            primaryTestFunction(param1, param2, param3);
            setTimeout(start, 10000);
        }else{
            clearTimeout(timeout_function);
        }
    }, 10000);
}

Alright, so the idea is that when I run testFunction() and it includes the action "start" the setTimeout() loop will begin and when I run testFunction() again but with action "stop" or anything else for that matter, it should break the loop by running clearTimeout() but that's not the case.好的,所以这个想法是,当我运行 testFunction() 并且它包含动作“开始”时,setTimeout() 循环将开始,当我再次运行 testFunction() 但动作“停止”或其他任何事情时,它应该通过运行 clearTimeout() 来打破循环,但事实并非如此。

I even placed another clearTimeout() outside timeout_function and within an if() statement to catch the stop action and run clearTimeout() but it doesn't work.我什至在 timeout_function 之外和 if() 语句中放置了另一个 clearTimeout() 来捕获停止操作并运行 clearTimeout() 但它不起作用。

I can start it and the loop runs accordingly but I cannot make it stop.我可以启动它,循环会相应地运行,但我无法让它停止。 Thanks in advance,提前致谢,

First, in case this is causing confusion, setTimeout does not return a function, but a integer id.首先,如果这引起混淆, setTimeout不会返回一个函数,而是一个整数 id。 When you pass that id to clearTimeout it will not execute callback function passed to setTimeout .当您将该 id 传递给clearTimeout它不会执行传递给setTimeout回调函数。 But clearTimeout will only clear the function associated with the id passed to it, not any other timers that may be concurrently active.但是clearTimeout只会清除与传递给它的 id 关联的函数,而不是可能同时处于活动状态的任何其他计时器。

So, on line 6, when you call setTimeout again, you aren't saving the id it returns anywhere, and therefore you have no way of clearing it, so it will execute regardless if you clear the setTimeout function on line 3.因此,在第 6 行,当您再次调用setTimeout时,您并没有保存它在任何地方返回的 id,因此您无法清除它,因此无论您是否清除了第 3 行的setTimeout函数,它都会执行。

Now for the question: it may be possible to what you are trying to do with timeouts, but I think it would be much easier to use the similar function setInterval .现在的问题是:您可以尝试使用超时来做什么,但我认为使用类似的函数setInterval会容易得多。 It works the exact same way as timeout, but it executes the function passed to it on indefinitely on an interval until it is cleared.它的工作方式与 timeout 完全相同,但它会在一个时间间隔内无限期地执行传递给它的函数,直到它被清除为止。

This way you could just这样你就可以

const intervalID = setInterval (() => {
                    primaryTestFunction(param1, param2, param3);
                   }, 10000);
                  

And then clearInterval(intervalID) when you want to stop it.然后在您想停止它时clearInterval(intervalID)

 var loop1 = setInterval(()=> { if(action == 'start'){ primaryTestFunction(param1, param2, param3); setTimeout(start, 10000); } else{ clearInterval(loop1); } },100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM