| | |
Is there a sleep/wait javascript function for firefox?
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Dec 2004
Posts: 1,655
Reputation:
Solved Threads: 35
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.
You could have it wait so many milliseconds, and then execute an empty procedure, I suppose.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
setTimeout("alert('hello')",1250);
You could have it wait so many milliseconds, and then execute an empty procedure, I suppose.
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:
You would implement this with:
This effective 'pauses' the execution of your code.
So if you had an algorithm like such:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function myFunction(){ doSomething(); wait(500); doSomethingElse(); }
You would implement this with:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function myFunction(){ doSomething(); setTimeout(function(){doSomethingElse();}, 500); }
This effective 'pauses' the execution of your code.
•
•
Join Date: Dec 2006
Posts: 6
Reputation:
Solved Threads: 0
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.
After a couple of google queries I found the only solution to sleep was to execute this function:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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 9:26 am. Reason: code section change
•
•
Join Date: Mar 2008
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Nov 2008
Posts: 1
Reputation:
Solved Threads: 0
This code will freeze any threads after it's execution, so this is a useless one:
This codes will NOT freeze any threads after it's execution but only works for Firefox:
javascript Syntax (Toggle Plain Text)
function jsWaitForDelay(delay) { var startTime = new Date(); var endTime = null; do { endTime = new Date(); } while ((endTime - startTime) < delay); }
This codes will NOT freeze any threads after it's execution but only works for Firefox:
javascript Syntax (Toggle Plain Text)
/** * Netscape compatible WaitForDelay function. * You can use it as an alternative to Thread.Sleep() in any major programming language * that support it while JavaScript it self doesn't have any built-in function to do such a thing. * parameters: * (Number) delay in millisecond */ function nsWaitForDelay(delay) { /** * Just uncomment this code if you're building an extention for Firefox. * Since FF3, we'll have to ask for user permission to execute XPCOM objects. */ netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); // Get the current thread. var thread = Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager).currentThread; // Create an inner property to be used later as a notifier. this.delayed = true; /* Call JavaScript setTimeout function * to execute this.delayed = false * after it finish. */ setTimeout("this.delayed = false;", delay); /** * Keep looping until this.delayed = false */ while (this.delayed) { /** * This code will not freeze your browser as it's documented in here: * https://developer.mozilla.org/en/Code_snippets/Threads#Waiting_for_a_background_task_to_complete */ thread.processNextEvent(true); } }
Regards,
Terry
Terry
•
•
Join Date: Mar 2009
Posts: 1
Reputation:
Solved Threads: 0
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.
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
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
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.
http://www.devcheater.com
http://www.devcheater.com
![]() |
Similar Threads
- database connection(select ,insert query) within javascript function (JSP)
- Javascript Function to reload DOM Element? (JavaScript / DHTML / AJAX)
- Writing a javascript function (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Want to disaply entire string values in alertbox
- Next Thread: HELP! to autoplay or setinterval to YUI carousel
| Thread Tools | Search this Thread |
acid2 ajax ajaxexample ajaxjspservlets array browser bug captchaformproblem cart checkbox child class close codes createrange() css cursor date debugger decimal dependent design disablefirebug dom dropdown editor element embed engine enter error events explorer ext file firefox focus form forms frameworks getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe images index internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl listbox maps masterpage math media menu mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent paypal pdf php position post problem programming progressbar prototype redirect runtime safari scale scriptlets scroll search security shopping size software toggle unicode w3c web wysiwyg \n






