/***************************************************** **
//
// 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.