468,507 Members | 1,577 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,507 developers. It's quick & easy.

Delay the execution of a function with parameters

I have tried using setTimeout to delay the execution of a function with:

setTimeout('function()', ms)

This works, but if the function has parameters:

setTimeout('function(p1, p2, p3)', ms)

it does not work.

Any ideas very much appreciated.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Aug 12 '05 #1
5 10917
"Roger Withnell" wrote
I have tried using setTimeout to delay the execution of a function with:

setTimeout('function()', ms)

This works, but if the function has parameters:

setTimeout('function(p1, p2, p3)', ms)

it does not work.


You need to move the parameter variable names out of the string, so they are
interpreted rather than taken literally when the string is passed from the
timeout to the executioner:

setTimeout('function(' + p1 + ', '+ p2 + ', ' + p3 + ')', ms)

hth
ivo
http://4umi.com/

Aug 12 '05 #2
simple.

setTimeout(function() { myFunc(a, b, c); }, ms);

enjoy.

Aug 12 '05 #3
ASM
Roger Withnell wrote:
I have tried using setTimeout to delay the execution of a function with:

setTimeout('function()', ms)

This works, but if the function has parameters:

setTimeout('function(p1, p2, p3)', ms)


setTimeout('function('+p1+','+p2+','+p3+')', ms)
--
Stephane Moriaux et son [moins] vieux Mac
Aug 12 '05 #4
"Other" <th**@that.so> writes:
"Roger Withnell" wrote
setTimeout('function(p1, p2, p3)', ms)


Bad choice of function name, since "function" is a keyword in Javascript.
I'll call it "theFunction" instead.
You need to move the parameter variable names out of the string, so they are
interpreted rather than taken literally when the string is passed from the
timeout to the executioner:

setTimeout('function(' + p1 + ', '+ p2 + ', ' + p3 + ')', ms)


That only works if the values converts to strings that are literals
representing the same value. That's generally not the case for
objects, nor for strings (i.e., it only works for numbers, booleans,
null and undefined). So, it's *not* a good idea in general.

Using variable names is better, but not much, since the variables need
to be in the scope where the string is evaluated, and that is the
global scope. So 'theFunction(p1,p2,p3)' *will* work, *if* p1, p2 and
p3 are the names of global variables.

The best alternative is to use a function as first argument to
setTimeout:
setTimeout(function(){theFunction(p1,p2,p3);}, ms);
It works in all modern browsers.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 13 '05 #5
ASM
Lasse Reichstein Nielsen wrote:

As I did give too this answer :
setTimeout('function(' + p1 + ', '+ p2 + ', ' + p3 + ')', ms)
That only works if the values converts to strings that are literals
representing the same value. That's generally not the case for
objects, nor for strings (i.e., it only works for numbers, booleans,
null and undefined). So, it's *not* a good idea in general.


I do think p1,2,3 are certainly names of variables
(this variables modified by main function before re-call to itself)
Using variable names is better, but not much, since the variables need
to be in the scope where the string is evaluated, and that is the
global scope. So 'theFunction(p1,p2,p3)' *will* work, *if* p1, p2 and
p3 are the names of global variables.
We can suppose they are not global
if not, I do not understand the why of question ...
The best alternative is to use a function as first argument to
setTimeout:
setTimeout(function(){theFunction(p1,p2,p3);}, ms);
that's the best way
It works in all modern browsers.


is that to say it will not work on my NC4.5 ?
--
Stephane Moriaux et son [moins] vieux Mac
Aug 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Jason Shohet | last post: by
4 posts views Thread by FAQ server | last post: by
1 post views Thread by maul581 | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.