JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

14.1 Timers

14.1 Timers


setTimeout() and setInterval() allow you to register a function to be invoked once or repeatedly after a specified amount of time has elapsed. These are important global functions of client-side JavaScript, and are therefore defined as methods of Window, but they are general-purpose functions and don’t really have anything to do with the window.

The setTimeout() method of the Window object schedules a function to run after a specified number of milliseconds elapses. setTimeout() returns a value that can be passed to clearTimeout() to cancel the execution of the scheduled function.

setInterval() is like setTimeout() except that the specified function is invoked repeatedly at intervals of the specified number of milliseconds:

setInterval(updateClock, 60000); // Call updateClock() every 60 seconds

Like setTimeout(), setInterval() returns a value that can be passed to clearInterval() to cancel any future invocations of the scheduled function.

Example 14-1 defines a utility function that waits a specified amount of time, invokes a function repeatedly, and then cancels the invocations after another specified amount of time. It demonstrates setTimeout(), setInterval(), and clearInterval().

Example 14-1. A timer utility function

/*

*Schedule an invocation or invocations of f() in the future.*Wait start milliseconds, then call f() every interval milliseconds,*stopping after a total of start+end milliseconds.*If interval is specified but end is omitted, then never stop invoking f.*If interval and end are omitted, then just invoke f once after start ms.*If only f is specified, behave as if start was 0.*Note that the call to invoke() does not block: it returns right away. */

function invoke(f, start, interval, end) {

if (!start) start = 0; // Default to 0 ms

if (arguments.length <= 2) // Single-invocation case

setTimeout(f, start); // Single invocation after start ms.

else { // Multiple invocation case

setTimeout(repeat, start); // Repetitions begin in start ms

function repeat() { // Invoked by the timeout above

var h = setInterval(f, interval); // Invoke f every interval ms.

// And stop invoking after end ms, if end is defined

if (end) setTimeout(function() { clearInterval(h); }, end);

} } }

For historical reasons, you can pass a string as the first argument to setTimeout() and setInterval(). If you do this, the string will be evaluated (as with eval()) after the specified timeout or interval. The HTML5 specification (and all browsers except IE) allow additional arguments to setTimeout() and setInterval() after the first two. Any such arguments are passed to the function that is invoked. If portability with IE is required, however, you shouldn’t use this feature.

If you call setTimeout() with a time of 0 ms, the function you specify is not invoked right away. Instead, it is placed on a queue to be invoked “as soon as possible” after any currently pending event handlers finish running.

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/27706.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1