Home > Web Front-end > JS Tutorial > body text

Use custom setTimeout and setInterval to pass parameters and object parameters_javascript skills

WBOY
Release: 2016-05-16 18:53:49
Original
1078 people have browsed it

/***************************************************** **
//
// Function: Modify window.setTimeout so that it can pass parameters and object parameters
// Usage method: window.setTimeout (callback function, delay time, parameter 1, parameter n )
//
************************************************ *************/
var mySetTimeOut = setTimeout;
window.setTimeout = function(callback, timeout)
{
var args = Array.prototype.slice.call(arguments, 2 );
function callFn(){callback.apply(null, args);}
return mySetTimeOut(callFn, timeout);
}
/***************************************************** **
//
// Function: Modify window.setInterval so that it can pass parameters and object parameters
// Usage method: window.setInterval (callback function, interval time, parameter 1, parameter n )
//
************************************************ *************/
var mySetInterval = setInterval;
window.setInterval = function(callback, interval)
{
var args = Array.prototype.slice.call(arguments, 2);
function callFn(){callback. apply(null, args);}
return mySetInterval(callFn, interval);
}
// The test code passes object
// There are no examples for ordinary parameters
var obj = { height: 40px;}
var testTimeout = testInterval = null;
function test(obj)
{
alert(obj.height);
clearSetTimeOut(testTimeout);
clearInterval( testInterval);
}
var testTimeout = window.setTimeout(test, 100, obj);
var testInterval = window.setInterval(test, 100, obj);
This function is compatible with IE and Firefox . And you can use clearSetTimeOut and clearInterval to clear, which is much more convenient than the original setTimeout and setInterval, and the parameter can be object.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template