开发者

Firefox setTimeout(func, ms) sending default parameters to callback

I am unable to find out more about this default parameter that I ran across and was hoping that someone could point out an explanation.

In Firefox (3.6 in this case) if you call the following code:

function test(someVar) {
   console.log('test ' + someVar);
}
setTimeout(test, 0);

It will log a "random" number to the console. I know you can pass parameters in Firefox like so:

setTimeout(test, 0, param1, param2);

But it appears as though Firefox is automatically sending a value. I think it's the number of ms past the requested call time but I cannot be certain. (EG: now() + 0ms == now(), but since it can't call right now it waits for the execution queue and returns the number of ms past that time?) If I put 500ms for the timeout it usually returns 0 unless I have a long running script behind it.

I also know that Firefox will clamp timeout requests to 10ms and putting in 0 will make it default to 10ms. If this value is a 'delay value' (ie: it took us 126ms longer than you requested) is it based on the value I enter(0)开发者_运维问答 or the clamped min?


One answer below suggests that this is the timer handle. The following code disproves that(?):

function test(someVar) {
   console.log('test ' + someVar);
}
console.log('Timer ' + setTimeout(test, 0));

This will return two different values.


Of course, it will return undefined in IE so I'm not writing code that expects it, but I was curious.

(This actually was causing a bug in some code I was working on that relied on optional parameters for a calling function. Worked in IE, not FF.)


From MDC

Gecko passes an extra parameter to the callback routine, indicating the "lateness" of the timeout in milliseconds.

Because the "actual" delay maybe longer than the delay specified in the setTimeoutcall, the "lateness" will be zero if the function was called exactly after the delay specified, non-zero otherwise.


You're correct that it's the offset. It'll usually be zero (meaning it was called when it should have been), but if the JS engine is backed up, it'll be higher, or it can even be negative.

reference: https://developer.mozilla.org/en/DOM/window.setTimeout

(see the part in yellow)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜