Connecting Tech Pros Worldwide Forums | Help | Site Map

SetTimeout & How to pass parameters to delayed function

Newbie
 
Join Date: Sep 2007
Posts: 31
#1: Dec 29 '07
Hi,

Can someone help me with the command format please.

I have a function (called fade) that was 3 parameters:
the first 2 are colours therefore formatted #123456
and the 3rd is a number of milliseconds.

A normal call to the function therefore looks like this:

Expand|Select|Wrap|Line Numbers
  1. fade("#FFFFFF", "#000000", 1000); 
// ie fades from white to black over 1 sec.
this works fine, but I now want to delay the start of the function.

So I tried;
Expand|Select|Wrap|Line Numbers
  1. setTimeout('fade("#FFFFFF", "#000000". 1000)' , 2000);
// to delay start by 2 secs
then realised that I needed to take care of the quotes .. so tried lots! of attempts like:
Expand|Select|Wrap|Line Numbers
  1. setTimeout('fade(\"#FFFFFF\", \"#000000\", 1000)', 2000)
but none of my attempts work.

Could someone guide me please.
Thanks
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#2: Dec 29 '07

re: SetTimeout & How to pass parameters to delayed function


setTimeout and setInterval will accept a function as the first argument-

setTimeout(function(){fade("#FFFFFF", "#000000")}, 1000);
Newbie
 
Join Date: Sep 2007
Posts: 31
#3: Dec 29 '07

re: SetTimeout & How to pass parameters to delayed function


Quote:

Originally Posted by mrhoo

setTimeout and setInterval will accept a function as the first argument-

setTimeout(function(){fade("#FFFFFF", "#000000")}, 1000);

Thanks, Unfortunately, it didnt work, but then I noticed the 3rd parm was missing, so I added the 3rd parameter to your suggestion as follows:

setTimeout(function(){fade("#FFFFFF", "#000000", 2000)}, 1000);

(I also tried wrapping the 2000 with "s (ie "2000"), but in neither case did it work.?
Newbie
 
Join Date: Sep 2007
Posts: 31
#4: Dec 29 '07

re: SetTimeout & How to pass parameters to delayed function


Correction:
It DOES now work with the 2000 wrapped in "s (ie "2000")

Thanks VERY much for your help. I would never have found this solution without your help!
Newbie
 
Join Date: Sep 2007
Posts: 31
#5: Dec 29 '07

re: SetTimeout & How to pass parameters to delayed function


I am using a function that fades the background between 2 colours over n secs.
It works fine.

I now need to delay the start of the fade - so I tried setTimeout, which (seemingly) allows a delay before the function is called.

eg If I set fade time to 2 secs, and delay_time to 5 secs ... it doesnt seem to work as intended.

Can someone help me understand how setTimeout works.
1) Does it delay the calling of the function for n secs?,
or
2) Does it delay the return from the function?

If 2) above, is there another solution I could use?

Thanks for your help.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Dec 29 '07

re: SetTimeout & How to pass parameters to delayed function


I've merged your threads because they relate to the same problem.

See if this link helps you to understand setTimeout. If not, post your code (including fade()).
Reply