setTimeout/setInterval执行的句柄函数在firefox下会附加一个参数

  使用setTimeout/setInterval绑定事件句柄时,firefox13-会自动给事件处理函数增加一个附加参数,所以做选项卡自动切换功能时,注意这2个函数的使用方法,可能会导致你程序出现bug。firefox14+已经去掉这个功能。

Note: Prior to Gecko 13 (Firefox 13.0 / Thunderbird 13.0 / SeaMonkey 2.10), Gecko passed an extra parameter to the callback routine, indicating the "actual lateness" of the timeout in milliseconds. This non-standard parameter is no longer provided.

https://developer.mozilla.org/en-US/docs/Web/API/window.setInterval?redirectlocale=en-US&redirectslug=DOM%2Fwindow.setInterval#Callback_arguments

 

  测试代码如下

<script>
    function timerhandler() {
        //chrome,IE输出0,firefox输出1,firefox下setTimeout/setInterval传递了一个参数,指示计时完成后这个函数被延时执行的时间,单位ms
        alert(arguments.length);
    }
    var timer = setTimeout(timerhandler, 2000);
</script>


  其中非IE核心浏览器(IE9+未测试),还支持传递参数。

<script>
    function timerhandler() {
        alert(arguments.length);//IE 0,firefox 3,chrome 2
        console.log(arguments)//chrome输出[1,2],firefox下输出[1,2,延时执行时间],比chrome多了延时执行时间
    }
    var timer = setTimeout(timerhandler, 2000,1,2);
</script>



  如果要兼容IE,要写成下面这种形式

<script>
    function timerhandler() {
        alert(arguments.length); 
        console.log(arguments)
    }
    var timer = setTimeout(function () { timerhandler(1, 2) }, 2000);
</script>

 

加支付宝好友偷能量挖...


原创文章,转载请注明出处:setTimeout/setInterval执行的句柄函数在firefox下会附加一个参数

评论(0)Web开发网
阅读(113)喜欢(0)JavaScript/Ajax开发技巧