简体   繁体   English

Javascript:无法停止setTimeout

[英]Javascript: Can't stop the setTimeout

I'm working on a proxy server checker and have the following code to start the requests at intervals of roughly 5 seconds using the setTimeout function; 我正在使用代理服务器检查程序并使用以下代码使用setTimeout函数以大约5秒的间隔启动请求;

        function check() {

            var url = document.getElementById('url').value;
            var proxys = document.getElementById('proxys').value.replace(/\n/g,',');

            var proxys = proxys.split(",");

            for (proxy in proxys) {

                var proxytimeout = proxy*5000;

                t = setTimeout(doRequest, proxytimeout, url, proxys[proxy]);

            }
        }

However I can't stop them once their started! 但是一旦他们开始我就无法阻止他们!

        function stopcheck() {

            clearTimeout(t);

        }

A fix or better method will be more that appreciated. 修复或更好的方法将更受欢迎。

Thank you Stack Overflow Community! 谢谢Stack Overflow社区!

There are 2 major problems with your code: 您的代码有两个主要问题:

  1. t is overwritten for each timeout, losing the reference to the previous timeout each iteration. 每次超时都会覆盖t ,每次迭代都会丢失对先前超时的引用。
  2. t is may not be a global variable, thus stopcheck() might not be able to "see" t . t可能不是全局变量,因此stopcheck()可能无法“看到” t

Updated functions: 更新功能:

function check() {
    var url         = document.getElementById('url').value;
    var proxys      = document.getElementById('proxys').value.replace(/\n/g,',');
    var timeouts    = [];
    var index;
    var proxytimeout;

    proxys = proxys.split(",");
    for (index = 0; index < proxys.length; ++index) {
        proxytimeout                = index * 5000;
        timeouts[timeouts.length]   = setTimeout(
            doRequest, proxytimeout, url, proxys[index];
        );
    }

    return timeouts;
}

function stopcheck(timeouts) {
    for (var i = 0; i < timeouts.length; i++) {        
        clearTimeout(timeouts[i]);
    }
}

Example of use: 使用示例:

var timeouts = check();

// do some other stuff...

stopcheck(timeouts);

Where is 't' being defined? 't'在哪里被定义? It keeps being redefined in the for loop, so you will loose track of each timeout handle... 它继续在for循环中重新定义,因此你将松开每个超时句柄的跟踪...

You could keep an array of handles: 你可以保留一个句柄数组:

var aTimeoutHandles = new Array();
var iCount = 0;
for (proxy in proxys) {

    var proxytimeout = proxy*5000;

    aTimeoutHandles[iCount++] = setTimeout(doRequest, proxytimeout, url, proxys[proxy]);

}

Define t outside of both functions first. 首先在两个函数之外定义t Additionally, you're overwriting t with each iteration your for loop. 此外,你覆盖t每次迭代您for循环。 Perhaps building a collection of references, and then to stop them you cycle through and clearTimeout on each. 也许构建一个引用集合,然后阻止它们在每个引用上循环和clearTimeout

You overwrite t each time you set the interval. 每次设置间隔时都会覆盖t Thus you only end up clearing the last one set. 因此,您最终只能清除最后一组。

You have a few problems there: 你有一些问题:

  1. The main one is that you're overwriting t on each iteration of your for loop; 其中主要的一个是,你覆盖t您的每一次迭代for循环; you need an array of t s for your structure to work. 你需要一个t的数组来让你的结构工作。
  2. You're using for..in to loop through the indexes of the array. 您正在使用for..in循环for..in数组的索引。 That's not what for..in is for (although there are a lot of people confused about that; see this article ). 这不是什么for..in是(虽然有很多人混淆有关; 看到这篇文章 )。 for..in loops through the property names of an object , not the indexes of an array , and therefore this usage breaks in non-trivial situations. for..in循环for..in 对象属性名称 ,而不是数组索引 ,因此这种用法在非平凡的情况下会中断。 Just use an old-fashioned for loop. 只需使用老式的for循环。
  3. You're declaring proxys twice. 你要两次声明proxys This is actually harmless, but... 这实际上是无害的,但......
  4. You're not declaring proxy at all (which isn't harmless; it becomes an implicit global). 你根本没有声明proxy (这不是无害的;它变成了一个隐含的全局)。

I've updated the code in Jordan's excellent answer to address those. 我已经更新了Jordan的优秀答案中的代码来解决这些问题。

Looks like you're setting multiple timeouts (one for each proxy), but trying to save them in the same variable. 看起来您正在设置多个超时(每个代理一个),但尝试将它们保存在同一个变量中。 You probably need to use an array there, instead of a simple variable. 您可能需要在那里使用数组,而不是简单的变量。

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

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