setTimeout("count()",1000);
setTimeout function can be used to call a function without any parameters easily, but is there ant way i can pass a parameter to function count inside function setTimeout ???
| SitePoint Sponsor |


setTimeout("count()",1000);
setTimeout function can be used to call a function without any parameters easily, but is there ant way i can pass a parameter to function count inside function setTimeout ???
chartahir
Like this?
Code:setTimeout("count('a string parameter',99)",1000);
We miss you, Dan Schulz.
Learn CSS. | X/HTML Validator | CSS validator
Dynamic Site Solutions
Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


yes thats like what i want but,when passing parameters to count through setTimeout the parameters are received as constant.
Can i pass variables ?
chartahir





"count(" + someVar + ")"
It's gonna be a string on the other side of the worm hole.





You can also pass a function (recommended) instead of a String as the first argument
Code:window.setTimeout(function() { count(someVar); }, 1000);
Really? If I pass a string like that I get errors. That would work for numbers, but they aren't passed as strings.Originally Posted by 7stud
That doesn't work in IE5/Mac or Safari 1.0 though.You can also pass a function (recommended) instead of a String as the first argument
Browsers other than IE and Safari 1.0 support more than 2 arguments to setTimeout. After the first two, the others will be passed as arguments to the function specified in the first argument, if you just pass a reference to a function (not a string or a function call) as the first argument.
We miss you, Dan Schulz.
Learn CSS. | X/HTML Validator | CSS validator
Dynamic Site Solutions
Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.





Ah, Safari 1 fails that, crap. IE 5/Mac is pretty dead though. Thank god. RIP.
If safari1 support is a must, you could push the function to a stack, with something like this:
I wouldn't care too much about archaic browsers though.Code:var Callback = { stack: [], setTimeout: function(fn, timeout) { var id = stack.length; Callback.stack[id] = fn; return window.setTimeout("Callback.__call("+id+")", timeout); }, __call: function(id) { var fn = Callback.stack[id]; Callback.stack[id] = null; fn(); } }; Callback.setTimeout(function() { count(someVar); }, 1000);
That's a very interesting alternative, kyberfabrikken. I hadn't thought of doing that. Did you come up with that yourself?
Safari 1.2+ supports it though. (I don't know about 1.1.)Originally Posted by Pepejeria
We miss you, Dan Schulz.
Learn CSS. | X/HTML Validator | CSS validator
Dynamic Site Solutions
Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.





You're right of course. I left out the "internal" quote marks that make it work:
The way that works is if you have:Code:"count('" + someVar + "')"
then the value for someVar is substituted in the code, giving you:Code:var someVar = 10;
then the concatenation happens, forming the string:Code:count(' + 10 + ')
Forcing js to evaluate the string immediately serves to freeze dry the current value of someVar into the setTimeout().Code:count('10')
Compare to:Code:function show(str) { alert(str); } var data1 = "a string"; var data2 = 10; setTimeout("show('" + data1 + "')", 1000); setTimeout("show('" + data2 + "')", 2000); //The following execute before the setTimeout(): data1 = "a different value"; data2 = 156;Code:function show(str) { alert(str); } var data1 = "a string"; var data2 = 10; setTimeout("show(data1)", 1000); setTimeout("show(data2)", 2000); data1 = "a different value"; data2 = 156;
Last edited by 7stud; Mar 15, 2007 at 03:25.
Bookmarks