开发者

Weird random value as default interval handler parameter

开发者 https://www.devze.com 2022-12-17 05:20 出处:网络
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:

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:

setInter开发者_如何学Pythonval(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


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.


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 );
0

精彩评论

暂无评论...
验证码 换一张
取 消