The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
SetTimeout arguments problem
Discuss SetTimeout arguments problem in the JavaScript Development forum on Dev Shed. SetTimeout arguments problem JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 2nd, 2006, 04:00 PM
|
 |
PHP is my Love!
|
|
Join Date: Jul 2003
Location: Kafr Sakr, Egypt
Posts: 87
Time spent in forums: 12 h 13 m 5 sec
Reputation Power: 10
|
|
|
SetTimeout arguments problem
Hello,
As we know that setTimeout() is used to call a function spontanousely over fixed parts of time as the following manner:
setTimeout(myFunc,1000);
The problem I got is, I want to call the function myFunc() with passing an argument to it as follows,
setTimeout(myFunc('parameter'),1000);
The above code generates an error. So how can I get my goal?
|

May 2nd, 2006, 04:26 PM
|
|
JackOfAllTrades, MasterOfNone
|
|
Join Date: Nov 2003
Location: Kentucky, USA
|
|
setTimeout() and setInterval() can take one of two things for their first argument. The first is, as you said, a function reference. The second is a string which gets eval()'ed. So in your example, you'd want to do this:
javascript Code:
Original
- javascript Code |
|
|
|
setTimeout("myFunc('parameter');", 1000);
If using variables as parameters to a delayed function, keep in mind the following distinction:
javascript Code:
Original
- javascript Code |
|
|
|
// calls myFunc() with the value param will have 1000ms from now setTimeout("myFunc(param);", 1000); // calls myFunc() with the current value of param, // even if it changes by the time myFunc() actually gets called setTimeout("myFunc(" + param + ");", 1000);
Last edited by jnsg : May 2nd, 2006 at 04:33 PM.
|

May 4th, 2006, 06:53 AM
|
 |
Contributing User
|
|
Join Date: Aug 2005
Location: Bucharest ROMANIA
|
|
Furthermore, it depends on the nature of your parameter, because the expresion is evaluated.
1. If the parameter is a number or a stringed number
var param =5
or
var param ='5'
and you need to pass it as a decimal number
the syntax is:
PHP Code:
setTimeout('myFunc('+param+')',3000)
2. But if the parameter is an alphanumeric
var param ='foo';
the parameter has to be quoted (to avoid evaluation) and the syntax becomes:
PHP Code:
setTimeout('myFunc("'+param+'")',3000)
In the first case as the parameter is evaluated, even if the variable is in fact a string, such as '5', when passed it becomes a decimal number.
Last edited by KorRedDevil : May 4th, 2006 at 06:56 AM.
|

May 4th, 2006, 08:21 AM
|
 |
PHP is my Love!
|
|
Join Date: Jul 2003
Location: Kafr Sakr, Egypt
Posts: 87
Time spent in forums: 12 h 13 m 5 sec
Reputation Power: 10
|
|
|
Conclusion
From the above threads, I may be able to conclude that:-
setTimeout() method, acts as eval() method in addition, it able to repeat the evaluation of the string every determined period of time passed.
|

May 4th, 2006, 11:06 AM
|
 |
Contributing User
|
|
Join Date: Aug 2005
Location: Bucharest ROMANIA
|
|
|
Quite so, except that you may avoid evaluation if you quote the parameter.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|