0
function checkDownload(a, b) {
    var number = a;
    var id = b;

    //NOT RELATED CODE....
    setTimeout("checkDownload(number,id)", 5000);
}
checkDownload("test", "test1");

So the thing is, at the setTimeout an Error comes with (Cannot find variable number).... But why? I only want to refresh the function after 5 seconds with the variable I got before.

Regards

1

1 Answer 1

9

So the think is, at the setTimeout an Error comes with (Cannot find variable number).... But why?

Because when you use a string with setTimeout, the code in that string is evaluated at global scope. If you don't have global number and id variables, you'll get an error.

Don't use strings with setTimeout and setInterval, use functions:

// On any recent browser, you can include the arguments after the timeout and
// they'll be passed onto the function by the timer mechanism
setTimeout(checkDownload, 5000, number, id);

// On old browsers that didn't do that, use a intermediary function
setTimeout(function() {
    checkDownload(number, id);
}, 5000);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.