奇怪的随机值作为默认间隔处理程序参数

时间:2010-01-21 02:50:49

标签: javascript firefox setinterval

刚刚遇到这个。它并没有真正影响任何事情,但我想知道它为什么会发生。

如果我在firefox上使用firebug运行以下代码:

setInterval(function(param) 
     {
        console.log("param is %o",param)
     },500);

param似乎被分配了一个模糊的随机值:

   param is -2
    param is -1
    param is -2
    param is 1
    param is -1
    param is 6
    param is -1
    param is 0
    param is -2
    param is 2
    param is 0
    param is 2
    param is 0
    param is 0
    param is 0
[..]
    param is 0
    param is 0
    param is 0
    param is 0
    param is 0
    param is 0
    param is 0
    param is 911
    param is 0
    param is 0
    param is 0
    param is -1

我很感激我没有将任何参数传递给setInterval以传递给函数,但为什么javascript选择传递这个随机数?

我原本期望未定义或类似的东西......

干杯

P.S。尚未在其他浏览器中测试

2 个答案:

答案 0 :(得分:3)

它似乎依赖于Firefox的CPU使用率。

我猜这是应该调用回调的延迟。

编辑I was right。这是调用回调后的毫秒数。

答案 1 :(得分:0)

这是由window.setInterval自动设置的区间ID。如果存储结果,则可以稍后清除间隔(停止)

var intID = window.setInterval( function(){ alert("I'm annoying!"); }, 10000 );

// this will kill it before it annoys you, :D
window.clearInterval( intID );

window.setTimeout

的情况也是如此
var timeID = window.setTimeout( function(){ alert("I'm annoying!"); }, 10000 );

// this will kill it before it annoys you, :D
window.clearTimeout( timeID );