FAQ Topic - How do I make a 10 second delay? (2013-06-14)

2 views
Skip to first unread message

FAQ server

unread,
Jun 13, 2013, 7:00:00 PM6/13/13
to
-----------------------------------------------------------------------
FAQ Topic - How do I make a 10 second delay?
-----------------------------------------------------------------------

There is no built-in way to pause execution in javascript such
as a sleep function, but hosts usually provide a method of some
form. Web browsers are designed for event driven programming and
only provide the `setTimeout` and `setInterval` functions
to facilitate timed delays. The delay before calling `getSnork` may
exceed the second parameter to `setTimeout` and `setInterval`
due to implementation differences among browsers.

To call the function `getSnork`, approximately 10 seconds
after the function `getMoomin()` completes, you would do this:

getMoomin();
setTimeout(getSnork, 10000);

Script execution is not stopped, and adding `getSnufkin()` after the
`setTimeout` line would immediately execute the function `getSnufkin`
before `getSnork`.

Achieving delays through running a loop of some sort for a pre-defined
period is a bad strategy, as that will inhibit whatever was supposed to
be happening during the delay, including blocking user interation.

Other (less event driven) hosts have different wait functions,
such as `WScript.Sleep()` in the Windows Script Host.

<URL: http://msdn.microsoft.com/en-us/library/ms536753%28VS.85%29.aspx>
<URL: http://docs.sun.com/source/816-6408-10/window.htm#1203758>
<URL: http://en.wikipedia.org/wiki/Event-driven_programming>
<URL: http://jibbering.com/faq/faq_notes/misc.html#mtSetTI>


The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/

--

The sendings of these daily posts are proficiently hosted
by http://www.pair.com.

Reply all
Reply to author
Forward
0 new messages