User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 391,615 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,620 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 40421 | Replies: 6
Reply
Join Date: Aug 2005
Location: Carmel, IN
Posts: 67
Reputation: Sailor_Jerry is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
Sailor_Jerry's Avatar
Sailor_Jerry Sailor_Jerry is offline Offline
Junior Poster in Training

Is there a sleep/wait javascript function for firefox?

  #1  
Jun 6th, 2006
I need to suspend processing in my script for a few seconds, and i am not sure how.

Thanks, sj
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Posts: 1,589
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 34
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Is there a sleep/wait javascript function for firefox?

  #2  
Jun 6th, 2006
There is no "sleep" or "wait" operator in JavaScript. You can set a timer, though, and when the timer expires, it will execute a function.

setTimeout("alert('hello')",1250);

You could have it wait so many milliseconds, and then execute an empty procedure, I suppose.
Reply With Quote  
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: Is there a sleep/wait javascript function for firefox?

  #3  
Jun 6th, 2006
What you usually do is what Tom has suggested, but you would invoke a call to the code that you want to execute after timeout.

So if you had an algorithm like such:

function myFunction(){
   doSomething();
   wait(500);
   doSomethingElse();
}

You would implement this with:

function myFunction(){
   doSomething();
   setTimeout(function(){doSomethingElse();}, 500);
}

This effective 'pauses' the execution of your code.
Reply With Quote  
Join Date: Aug 2005
Location: Carmel, IN
Posts: 67
Reputation: Sailor_Jerry is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
Sailor_Jerry's Avatar
Sailor_Jerry Sailor_Jerry is offline Offline
Junior Poster in Training

Re: Is there a sleep/wait javascript function for firefox?

  #4  
Jun 6th, 2006
Just what i needed.

Thanks guys, sj
Reply With Quote  
Join Date: Dec 2006
Posts: 6
Reputation: nemo5 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nemo5 nemo5 is offline Offline
Newbie Poster

Re: Is there a sleep/wait javascript function for firefox?

  #5  
Dec 31st, 2006
This solution is not really a sleep. It effectively postpones the execution of function called by the setTimeout method, but javasctipy is asynchronous, so the execution of the commands following the setTimeout method will happend immediately after.
After a couple of google queries I found the only solution to sleep was to execute this function:

function pause(millis) 
{
        var date = new Date();
        var curDate = null;

        do { curDate = new Date(); } 
        while(curDate-date < millis)
} 



Problem with this approach is when you need a few of shor sleeps one after another. In this case the only thing that happends is the cpu usage is gonna hit the roof and the script gets paused till the last pause is executed.
Last edited by nemo5 : Dec 31st, 2006 at 8:26 am. Reason: code section change
Reply With Quote  
Join Date: Jan 2006
Posts: 1
Reputation: Mark Whitt is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Mark Whitt Mark Whitt is offline Offline
Newbie Poster

Re: Is there a sleep/wait javascript function for firefox?

  #6  
Jan 17th, 2008
Thanks nemo5 for your input! I needed a way to cause a method to stop in its tracks so the calling method didn't think the currentl one was done yet, and this should do it.
Reply With Quote  
Join Date: Mar 2008
Posts: 1
Reputation: kanian77 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
kanian77 kanian77 is offline Offline
Newbie Poster

Re: Is there a sleep/wait javascript function for firefox?

  #7  
Mar 21st, 2008
The two approaches (do while, and dosomething/settimeout(dosomeelse)) are used in different situations.
1. do while
You just want to pause, but are not expecting anything to happen in the browser while waiting (no document loading, no event). The javascript interpreter will be busy executing the while loop.
(Check http://www.sean.co.uk/a/webdesign/javascriptdelay.shtm)
2. dosomething/settimeout(dosomeelse)
If you want your code to wait for something to happen, like a dom node being created or loaded you have to resort to other tactics.
Joseph Moore at http://40withegg.com/2007/1/5/a-non-...ript-wait-loop calls it a non-cpu intensive wait. Meaning the browser can still function and thus perform job that your code is waiting on.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 12:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC