views:

41

answers:

2

Hi,

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

+3  A: 

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.

SLaks
Well done. Makes sense. Seems to be always added as an additional parameter.So if 2 arguments as passed, that value is the third one..
Ben
yeah, I was just on that page too
Dan Beam
yeah, good find, definitely. didn't know this. very cool
Dan Beam
A: 

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 );
Dan Beam
You're misunderstanding his question. He's not asking what `setInterval` returns; he's asking what it passes to the callback.
SLaks
yeah, my bad, just realized this. that is weird.
Dan Beam
this is the wrong answer, but maybe somebody doesn't know this either, so whatevs
Dan Beam