setTimeout calling function with parameters

J

JellyON

Hi. I'm a little bit lost trying to insert a setTimeout() for recursive
call containing string and numeric parameters.

Here is a generic example of what I would like to succeed to do :

function Stuff(a_str, an_int, delay)
{
// Three useful vars for managing the recursivity
var stop = false; // will be true when no more recurse needed
var command; // just to concatenate the recurse expression
var timer; // timeout identifier

// ... doesn't matter the treatment ...

// Recursivity part
if (stop)
clearTimeout(timer);
else
{
command = "Stuff('" + a_str + "'," + an_int + "," + delay + ")";
timer = setTimeout(command, delay);
}
}

And it doesn't work.

I've searched in the Web and ng, tried different syntax about command
(for example, adding a ";" at the end of the command, using the
'+an_int+' syntax, etc), but doesn't succeed.

Well, I need your advice :)
 
R

RobG

JellyON said:
Hi. I'm a little bit lost trying to insert a setTimeout() for recursive
call containing string and numeric parameters.

Here is a generic example of what I would like to succeed to do :

function Stuff(a_str, an_int, delay)
{
// Three useful vars for managing the recursivity
var stop = false; // will be true when no more recurse needed
var command; // just to concatenate the recurse expression
var timer; // timeout identifier

// ... doesn't matter the treatment ...

// Recursivity part
if (stop)
clearTimeout(timer);
else
{
command = "Stuff('" + a_str + "'," + an_int + "," + delay + ")";
timer = setTimeout(command, delay);

Try:

timer = setTimeout( function() {
Stuff(a_str, an_int, delay);
}, delay);

Be careful - your variables are evaluated when the timer runs and may
be different to the values they had when you set them. Read up on
closures.

If 'delay' is constant, would setInterval be better?

[...]
 
J

JellyON

JellyON said:
Hi. I'm a little bit lost trying to insert a setTimeout() for recursive
call containing string and numeric parameters.

Here is a generic example of what I would like to succeed to do :

function Stuff(a_str, an_int, delay)
{
// Three useful vars for managing the recursivity
var stop = false; // will be true when no more recurse needed
var command; // just to concatenate the recurse expression
var timer; // timeout identifier

// ... doesn't matter the treatment ...

// Recursivity part
if (stop)
clearTimeout(timer);
else
{
command = "Stuff('" + a_str + "'," + an_int + "," + delay + ")";
timer = setTimeout(command, delay);

Try:

timer = setTimeout( function() {
Stuff(a_str, an_int, delay);
}, delay);

Be careful - your variables are evaluated when the timer runs and may
be different to the values they had when you set them. Read up on
closures.

If 'delay' is constant, would setInterval be better?

[...]

Thanks RobG, I've found too in my side : it was a simple error about
quotes in my command. I've found-it just after writing of my post. Also,
sure your solution works too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top