View Full Version : Delay
premshree
09-19-2002, 08:42 PM
I am calling a function like this :
setTimeout("move('"+noDisk+"')",2000);
noDisk is the argument the function move() takes.
I want to pass (noDisk-1) as the argument. How do I do that?
Also, is it possible to create a delay that waits for some time doing nothing and then executes the next instruction?
RadarBob
09-19-2002, 09:21 PM
Uh,...
I don't think it's possible to pass a function call as a string as a parameter to another function, and then have that passed function execute.
But you can "nest" functions, which are evaluated "inside-out", i.e. inner most function is executed first. BUT the inner function(s) - the move() function in your case - must return something. So you could do this:
setTimeout(move('"+noDisk+"'),2000);
Which could work assuming move() itself returns something that's appropriate as the first parameter in the setTimeout() function.
mordred
09-19-2002, 11:36 PM
Originally posted by RadarBob
Uh,...
I don't think it's possible to pass a function call as a string as a parameter to another function, and then have that passed function execute.
Hi RadarBob, that's roughly what's happening in the setTimeout() and setInterval() functions ;). Trying your suggestion gives me in Mozilla a error message which made smile:
Error: useless setTimeout call (missing quotes around argument?)
You were right of course on the general nature on how to "nest" expressions within each other. Only that it's not doable with the setTimeout() function, because it does expect a string parameter that indicates which function to call after the specified micorseonds delay.
premshree, I'm not quite sure I understand your request correctly: Do you want to pass a decremented number value to the function or the string value "noDisk - 1"? For the number way, you could do
var noDisk = 23;
function move(par) {
alert(par);
}
setTimeout("move(" + (noDisk - 1) + ")", 2000);
That will alert "22" after 2 secs.
And I fear the setTimeout() and setInterval() functions are the only ones that let you control the delay of a script practically. You could insert a useless for-loop that iterates over a large number doing nothing, but this kludge is not scalable since the execution is determined by the users machine capabilities (CPU and such).
adios
09-20-2002, 01:06 AM
Timer strings are presumably run through window.eval(). You can test for validity like this:
var noDisk = 23;
function move(par) {
alert(par);
}
eval("move(" + (noDisk - 1) + ")");
http://www.faqts.com/knowledge_base/view.phtml/aid/1602/fid/143
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.