为什么我的函数调用应该通过setTimeout立即执行? [英] Why is my function call that should be scheduled by setTimeout executed immediately?

查看:218
本文介绍了为什么我的函数调用应该通过setTimeout立即执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题。我有这个函数来测试代理服务器。
$ b $ pre $函数crawl(){
var oldstatus = document.getElementById('状态)的innerHTML。
document.getElementById('status')。innerHTML = oldstatus +Crawler Started ...< br />;
var url = document.getElementById('url').value;
var proxys = document.getElementById('proxys').value.replace(/ \\\
/ g,',');

var proxys = proxys.split(,);

(代理在proxys中){
var proxytimeout = proxy * 10000;
setTimeout(doRequest(url,proxys [proxy]),proxytimeout);




$ b我希望'doRequest()'函数是大约每隔10秒调用一次,但即使使用setTimeout()函数也会立即调用。

欢迎任何想法,谢谢。



PS:即使我为proxytimout设置了一个任意值,也没有任何作用。

你以这种形式给setTimeout函数,函数被执行而不是传递给setTimeout。你有三种选择使它工作:



先给函数,然后是超时和参数作为最后一个参数:

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

或者只写一个字符串来进行评估:

  setTimeout('doRequest('+ url +','+ proxys [proxy] +')',proxytimeout); 

第三种方式是传递一个匿名函数来调用函数。请注意,在这种情况下,您必须在闭包中执行此操作,以防止循环中的值发生变化,所以会变得棘手:

 < code $($ {
setTimeout(function(){doRequest(u,p);},t);
})(url,proxys [proxy] ,proxytimeout);

第二种格式有点怪异,但是如果参数是标量值(字符串,整数等等)。第三种格式有点不清楚,所以在这种情况下,第一种选择显然是最适合你的。


Here's my issue. I have this function to test proxy servers.

function crawl() {
    var oldstatus = document.getElementById('status').innerHTML;
    document.getElementById('status').innerHTML = oldstatus + "Crawler Started...<br />";
    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*10000;
        setTimeout(doRequest(url,proxys[proxy]), proxytimeout);
    }
}

I want the 'doRequest()' function to be called in roughly 10 second intervals but even with the setTimeout() the functions are called immediately.

Any ideas are welcome, thanks.

PS: Even if I put an arbitrary value for 'proxytimout' it has no effect.

解决方案

As you give the function to the setTimeout in that form, the function is executed instead of passed to the setTimeout. You have three alternatives to make it work:

Give first the function, then the timeout and the parameters as the last arguments:

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

Or just write a string that will be evaluated:

setTimeout('doRequest('+url+','+proxys[proxy]+')', proxytimeout);

Third style is to pass an anonymous function that calls the function. Note that in this case, you have to do it in a closure to prevent the values from changing in the loop, so it gets a bit tricky:

(function(u, p, t) {
    setTimeout(function() { doRequest(u, p); }, t);
})(url, proxys[proxy], proxytimeout);

The second format is a bit hacky, but works nevertheless if the arguments are scalar values (strings, ints etc). The third format is a bit unclear, so in this case the first option will obviously work best for you.

这篇关于为什么我的函数调用应该通过setTimeout立即执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆