This question already has an answer here:
- Calling functions with setTimeout() 5 answers
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.
You're misunderstanding the
setTimeout
function.The
setTimeout
function takes a function and executes it later.By writing
setTimeout(doRequest(url,proxys[proxy]), proxytimeout)
, you're _calling thedoRequest
function (immediately), and passing the result (assuming that it returns another function) tosetTimeout
.You need to pass
doRequest
's parameters tosetTimeout
, like this:This will pass
setTimeout
thedoRequest
function itself (without calling it first), and will also pass it the parameters to give todoRequest
when it finally calls it.This line here is the problem:
Writing
doRequest()
is actually calling the function. What you want is to pass the function itself: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:
Or just write a string that will be evaluated:
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:
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.