简体   繁体   English

停止循环功能setTimeout

[英]Stop loop function setTimeout

I have a loop using setTimeout like bellow: 我有一个使用setTimeout的循环,如下所示:

<button onclick="clickStop()">stop</button>

<script type="text/javascript">
    let i = 0;
    let sett;
    function timeOut(counter) {   
        sett = setTimeout(function () {
            $.get("./test3.php", {data_id: counter}, (data) => {
                console.log(data);
                i++;
                timeOut(i);
            });
        }, 1000);
        if(counter >= 10) {
            clearTimeout(sett);
            alert("Done!");
        }
    }
    timeOut(i);
    function clickStop() {
        clearTimeout(sett);
    }
</script>

But when i click button stop it's not working, help me!! 但是当我点击停止按钮不起作用时,请帮助我!

You can clear the timeout and also set a flag that will alert the callback in $.get that it shouldn't call timeOut() when it returns: 您可以清除超时并设置一个标志,该标志将警告$.get中的回调,当它返回时,它不应调用timeOut()

 let i = 0; let sett; let active = true // should the loop continue? function timeOut(counter) { if (counter < 10){ sett = setTimeout(function () { $.get("./test3.php", {data_id: counter}, (data) => { console.log(data); i++; if (active) timeOut(i); // only if active }); }, 1000); } else { alert("Done!"); } } timeOut(i); function clickStop() { active = false // stop loop clearTimeout(sett); // and clear current timeout } 

Your JS seems to be working just fine, see below, the counter increments and stop when I click on the stop button. 您的JS似乎运行良好,请参见下文,当我单击“停止”按钮时,计数器会增加并停止。 the issue is with $.get("./test3.php" ... 问题是与$.get("./test3.php" ...

can you elaborate on what the expected response of the ajax call is? 您能详细说明一下ajax调用的预期响应是什么吗?

Also if you can describe what you're trying to do, we can give you some pointers on best practices, because as those who commented have said, it's generally not a good idea to run XHR/ajax calls in loops 另外,如果您可以描述您要做什么,我们可以为您提供一些最佳实践的指导,因为正如评论的人所述,在循环中运行XHR / ajax调用通常不是一个好主意。

 <button onclick="clickStop()">stop</button> <script type="text/javascript"> let i = 0; let sett; function timeOut(counter) { sett = setTimeout(function () { timeOut(counter+1); console.log(counter); }, 1000); if(counter >= 10) { clearTimeout(sett); alert("Done!"); } } timeOut(i); function clickStop() { clearTimeout(sett); } </script> 

The timeout(i) you have inside the $.get response handler, would restart the loop even if you stopped it. $.get响应处理程序中的timeout(i)会重新启动循环,即使您已将其停止。 You can use a flag to get control over it. 您可以使用标志来控制它。

 let i = 0; let sett; let status = true; let url = "http://placekitten.com/200/300"; function timeOut(counter) { sett = setTimeout(function () { $.get(url, (data) => { console.log(i); i++; // repeat if status flag is true if(status) timeOut(i); }); }, 1000); if(counter >= 10) { clearTimeout(sett); status = false; // set status flag to false alert("Done!"); } } function clickStop() { clearTimeout(sett); status = false; // set status flag to false } timeOut(i); 
 <button onclick="clickStop()">stop</button> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

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

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