English 中文(简体)
Weird random value as default interval handler parameter
原标题:

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

最佳回答

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




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签