Re: Is there a sleep/wait javascript function for firefox?
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:
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 9:26 am. Reason: code section change
Re: Is there a sleep/wait javascript function for firefox?
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.
Re: Is there a sleep/wait javascript function for firefox?
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.
Re: Is there a sleep/wait javascript function for firefox?
I realize this is a pretty old thread, but it is easily found in Google.
Any approach that uses a while loop to wait is never going to be the best answer. We often refer to this technique as "spin wait" because it causes the cpu to keep crunching away at a process, sometimes stopping the entire machine (especially on older computers) meaning that your javascript wait could be intense enough for some users that their computing experience waits with you. This is not optimal.
While Javascript unfortunately does not provide a method of actually pausing the thread, setTimeout is your best bet. If there's any way that you can go to the trouble of implementing AlphaFoobar's method, then you 100% certainly should.
Last edited by tigerofdoom; Mar 27th, 2009 at 9:51 pm. Reason: poor wording, missed comma
Re: Is there a sleep/wait javascript function for firefox?
Here is a website that has implemented a javascript sleep function in a few different ways. It also shows what methods are compatible with what browsers and operating systems.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.