943,852 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jun 6th, 2006
-1

Is there a sleep/wait javascript function for firefox?

Expand Post »
I need to suspend processing in my script for a few seconds, and i am not sure how.

Thanks, sj
Similar Threads
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
Sailor_Jerry is offline Offline
88 posts
since Aug 2005
Jun 6th, 2006
0

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

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.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. setTimeout("alert('hello')",1250);

You could have it wait so many milliseconds, and then execute an empty procedure, I suppose.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Jun 6th, 2006
1

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

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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function myFunction(){
  2. doSomething();
  3. wait(500);
  4. doSomethingElse();
  5. }

You would implement this with:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function myFunction(){
  2. doSomething();
  3. setTimeout(function(){doSomethingElse();}, 500);
  4. }

This effective 'pauses' the execution of your code.
Reputation Points: 20
Solved Threads: 5
Junior Poster
alpha_foobar is offline Offline
182 posts
since May 2005
Jun 7th, 2006
0

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

Just what i needed.

Thanks guys, sj
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
Sailor_Jerry is offline Offline
88 posts
since Aug 2005
Dec 31st, 2006
0

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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function pause(millis)
  2. {
  3. var date = new Date();
  4. var curDate = null;
  5.  
  6. do { curDate = new Date(); }
  7. while(curDate-date < millis)
  8. }


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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nemo5 is offline Offline
6 posts
since Dec 2006
Jan 17th, 2008
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Mark Whitt is offline Offline
2 posts
since Jan 2006
Mar 21st, 2008
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kanian77 is offline Offline
1 posts
since Mar 2008
Nov 23rd, 2008
0

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

This code will freeze any threads after it's execution, so this is a useless one:
javascript Syntax (Toggle Plain Text)
  1. function jsWaitForDelay(delay) {
  2. var startTime = new Date();
  3. var endTime = null;
  4. do {
  5. endTime = new Date();
  6. } while ((endTime - startTime) < delay);
  7. }

This codes will NOT freeze any threads after it's execution but only works for Firefox:
javascript Syntax (Toggle Plain Text)
  1. /**
  2.  * Netscape compatible WaitForDelay function.
  3.  * You can use it as an alternative to Thread.Sleep() in any major programming language
  4.  * that support it while JavaScript it self doesn't have any built-in function to do such a thing.
  5.  * parameters:
  6.  * (Number) delay in millisecond
  7. */
  8. function nsWaitForDelay(delay) {
  9. /**
  10.   * Just uncomment this code if you're building an extention for Firefox.
  11.   * Since FF3, we'll have to ask for user permission to execute XPCOM objects.
  12.   */
  13. netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  14.  
  15. // Get the current thread.
  16. var thread = Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager).currentThread;
  17.  
  18. // Create an inner property to be used later as a notifier.
  19. this.delayed = true;
  20.  
  21. /* Call JavaScript setTimeout function
  22.   * to execute this.delayed = false
  23.   * after it finish.
  24.   */
  25. setTimeout("this.delayed = false;", delay);
  26.  
  27. /**
  28.   * Keep looping until this.delayed = false
  29.   */
  30. while (this.delayed) {
  31. /**
  32.   * This code will not freeze your browser as it's documented in here:
  33.   * https://developer.mozilla.org/en/Code_snippets/Threads#Waiting_for_a_background_task_to_complete
  34.   */
  35. thread.processNextEvent(true);
  36. }
  37. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
terrybleger is offline Offline
1 posts
since Nov 2008
Mar 27th, 2009
0

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tigerofdoom is offline Offline
1 posts
since Mar 2009
Oct 4th, 2009
-1

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.

http://www.devcheater.com
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jsreader is offline Offline
1 posts
since Oct 2009

This thread is more than three months old

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.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Loading Gallery/Flash using AJAX
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Jquery event 'e' and mouse coordinates





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC