PDA

View Full Version : Question about setTimeout



mburt
01-26-2007, 09:25 PM
Just, a question, but when a function has arguments how come you can't do setTimeout?

Example:

function getCode(arg1,arg2) {
//code
setTimeout("getCode()",100);
}

and when you call it in the body, it won't work? I'm really confused as to how to do this. :) Thanks in advance

mburt
01-26-2007, 09:31 PM
I even tried escaping the objects like this:

setTimeout("getCode(\""+arguments[0]+"\",\""+arguments[1]+"\")")
And still no good.

Twey
01-26-2007, 09:36 PM
Code passed as a string to setTimeout() or setInterval() is evaluated in the global scope. Create a closure:
setTimeout(function() { getCode(arg1, arg2); }, 100);