Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Something like sleep function

Status
Not open for further replies.

arunjp

Programmer
Jan 10, 2001
29
US
Hi !
Is there any way for a javascript statement to get executed, only after another function is finished, or a functionality something like "sleep" function. Sample code is appreciated.

Thanks
AJP
 
Well, you could call the function at the end of the function that you want it to execute after -- that would probably serve the purpose...

and javascript has a function called setTimeout that works like this:

setTimeout(functionOrStatement, milliseconds)

so that if I wanted a function, doSomething(), to fire 3 seconds after the page finished loading, I would say this:

<body onLoad=&quot;setTimeout('doSomething();', 3000);&quot;>

Where the functionOrStatement part of the function is any valid javascript statement or function.

But, for this particular question, I'd probably stick to calling the function inside (and at the end, no less) of the function that you want to finish executing before you call the other...

hope it helps! :)
Paul Prewett
 
//sleep function. stops execution for specified milliseconds.
function sleep(num)
{
var f = new Date();
f = new Number(f);
f = f + num;
while(true)
{
var n = new Date();
n = new Number(n);
if(n >= f)
{
break;
}
}
} luciddream@subdimension.com
 
luciddream, what does the processor on the machine that runs this code look like while this function is &quot;sleeping&quot;?

Isn't it pegged out?

What is the advantage of using this code over using something like the setTimeout() function? Would the setTimeout not be friendlier on the processor?

I'm curious to know because whenever I try to make a function (or code) &quot;sleep&quot;, I try not to use that while looping because it just cycles like mad -- eating up my resources.

thx
paul
 
well, the only advantage to using a sleep function is that if you want a pause in the middle of your code, you don't have to put the rest of your code in a separate function. also, setTimeout()'s don't wait for you to finish with modal components.... for example... try these two pieces of code...


for(var i = 0; i < 5; i++)
{
setTimeout('alert()', 5000);
}

and:

for(var i = 0; i < 5; i++)
{
alert()
sleep(5000);
}

the second one actaully waits five seconds between alerts, where the first pops them all up one after the other.

its not very practical or efficient, but, it does work. luciddream@subdimension.com
 
Thanks alot. Another question, is there any optimal value for the timeinterval for setTimeout or setInterval method, so, that would not cause a problem in low config systems.

AJP
 
I agree with link9 that setTimeout seems preferable to a loop. Would the following work?

var count=5; // using the given example
var num=5000;
var test;

// Does it need &quot;test&quot;?
function sleep() {
if (count>0) {
alert();
count--;
test=setTimeout(&quot;sleep();&quot;,num);
}
else clearTimeout(test);
}

// or would this be sufficient?
function sleep() {
if (count>0) {
alert();
count--;
setTimeout(&quot;sleep();&quot;,num);
}
}
 
Beautiful --

That's nice, engcomp

No eating of resources -- effectively putting the whole thing to sleep. :)

**copies and pastes**

paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top