473,386 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

setTimeout calling function with parameters

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 :-)
Jul 23 '05 #1
2 7657
JellyON wrote:
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?

[...]

--
Rob
Jul 23 '05 #2
In article <42d79301$0$10851$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, rg***@iinet.net.auau says...
JellyON wrote:
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.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
18
by: Frances Del Rio | last post by:
this code is supposed to flip imgs at intervals of one second, but it flipps them much faster, it seems it flips them all in that first second, the more seconds the faster it flips them, instead of...
2
by: Athanasius | last post by:
Could someone shed some light as to why the following setTimeout function will not work on the Mac IE5.2? It does however work on PC(Forefox,Netscape,IE) & Mac(Safari,Firefox). Here is the script,...
4
by: Telmo Costa | last post by:
I have a HTML page with a form with 2 buttons like this .... <input type="button" value="Add" onClick="ClickAdd();"/> <input type="button" value="Reset" onClick="ClickReset();"/> .... I...
4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
1
by: Chris Moltisanti | last post by:
Hey, I am using the setTimeout() function in IE to pause the execution of another funciton for a few seconds. My function is as follows: funtion test(e) { // the parameter 'e' is the event...
10
by: Shadow Lynx | last post by:
That subject packs a whallop, so let me explain in better detail what's happening and how it relates to ASPX pages... In a nutshell, if the first <script /on a page is of type "text/vbscript",...
18
by: barry | last post by:
I'm having a problem creating a delay before each list item gets moved ( http://www.polisource.com/PublicMisc/list-splitter-timeout-bug.html ). Choose any number of columns, vertical layout, and...
6
by: daveyand | last post by:
Hey Guys, I've stumped. I created a function that does various things to select boxes. Namely Get All selected indexes, populate array with these values
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.