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

发布于 2024-08-18 05:40:08 字数 814 浏览 7 评论 0原文

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

如果我在带有 firebug 的 Firefox 中运行以下代码:

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.没有在其他浏览器中测试过

Just came across this. It's not affecting anything really but i'm wondering why it's happening.

If I run the following code in firefox with firebug on:

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

param seems to be assigned a vaguely random value:

   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

I do appreciate that I'm not passing any argument to setInterval to pass on to the function, but why does javascript chooses to pass this random number ?

I would have expected undefined or something like that...

Cheers

p.s. Haven't tested in other browsers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

调妓 2024-08-25 05:40:08

它似乎取决于 Firefox 的 CPU 使用率。

我猜想这是从应该调用回调的时间开始的延迟。

编辑我是对的。这是调用回调的延迟毫秒数。

It appears to be dependent upon Firefox's CPU usage.

I would guess that it's the delay from when the callback should have been called.

EDIT: I was right. It's the number of milliseconds late the callback was called.

落在眉间の轻吻 2024-08-25 05:40:08

它是由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 );

it's an interval ID automatically set by window.setInterval. if you store the result, you can clear the interval later (to stop it)

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

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

this is also the case with window.setTimeout:

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

// this will kill it before it annoys you, :D
window.clearTimeout( timeID );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文