Use custom setTimeout and setInterval to pass Parameters and object parameter_javascript skills

Source: Internet
Author: User
This function is compatible with ie and firefox. In addition, clearSetTimeOut and clearInterval can be used for clearing. Compared with the original setTimeout, setInterval is much more convenient and the parameter can be an object. /*************************************** ***************
//
// Function: Modify window. setTimeout to pass Parameters and object parameters.
// Usage: 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 to pass Parameters and object parameters.
// Usage: window. setInterval (callback function, interval, 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 );
}
// Test the code to pass the object
// General parameters are not used as an example.
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. In addition, clearSetTimeOut and clearInterval can be used for clearing. Compared with the original setTimeout, setInterval is much more convenient and the parameter can be an object.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.