• 使用自定义setTimeout和setInterval使之可以传递参数和对象参数


    转载自http://www.jb51.net/article/17859.htm

    /****************************************************** 
    // 
    // 功能: 修改window.setTimeout,使之可以传递参数和对象参数 
    // 使用方法: window.setTimeout(回调函数,延迟时间,参数1,参数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); 

    /****************************************************** 
    // 
    // 功能: 修改window.setInterval,使之可以传递参数和对象参数 
    // 使用方法: window.setInterval(回调函数,间隔时间,参数1,参数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); 

    // 测试代码传递object 
    // 普通参数就不举例了 
    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); 
    该函数兼容ie,firefox。并且可以使用clearSetTimeOut和clearInterval清除,比原setTimeout,setInterval方便很多,并且参数可以是object。

  • 相关阅读:
    课程作业02
    课后作业01
    大道至简第一章伪代码
    《大道至简》读后感
    Codeforces 959 F. Mahmoud and Ehab and yet another xor task
    Codeforces 992 E. Nastya and King-Shamans
    Codeforces 835 F. Roads in the Kingdom
    Codeforces 980 D. Perfect Groups
    洛谷 P4315 月下“毛景树”
    JDOJ 1234: VIJOS-P1052 高斯消元
  • 原文地址:https://www.cnblogs.com/happycat1988/p/3440698.html
Copyright © 2020-2023  润新知