Firefox setTimeout(func, ms) 向回调发送默认参数
我无法找到有关我遇到的这个默认参数的更多信息,并希望有人能指出解释。
在 Firefox(本例中为 3.6)中,如果调用以下代码:
function test(someVar) {
console.log('test ' + someVar);
}
setTimeout(test, 0);
它会将“随机”数字记录到控制台。我知道你可以像这样在 Firefox 中传递参数:
setTimeout(test, 0, param1, param2);
但看起来 Firefox 会自动发送一个值。我认为这是请求的调用时间之后的毫秒数,但我不能确定。 (例如:now() + 0ms == now(),但由于它现在无法调用,所以它会等待执行队列并返回过去的毫秒数?)如果我将超时设置为 500ms,它通常会返回0 除非我背后有一个长时间运行的脚本。
我还知道 Firefox 会将超时请求限制为 10 毫秒,输入 0 将使其默认为 10 毫秒。如果这个值是一个“延迟值”(即:我们花了比您要求的时间长 126 毫秒),它是基于我输入的值(0)还是钳制的最小值?
下面的一个答案表明这是计时器句柄。以下代码反驳了这一点(?):
function test(someVar) {
console.log('test ' + someVar);
}
console.log('Timer ' + setTimeout(test, 0));
这将返回两个不同的值。
当然,它会在 IE 中返回 undefined
,所以我没有编写期望它的代码,但我很好奇。
(这实际上导致了我正在处理的一些代码中的一个错误,这些代码依赖于调用函数的可选参数。在 IE 中工作,而不是在 FF 中。)
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自MDC
由于“实际”延迟可能比 setTimeout 调用中指定的延迟长,因此如果在指定的延迟之后恰好调用该函数,“迟到”将为零,否则为非零。
From MDC
Because the "actual" delay maybe longer than the delay specified in the
setTimeout
call, the "lateness" will be zero if the function was called exactly after the delay specified, non-zero otherwise.你是对的,这是偏移量。它通常为零(意味着它在应该被调用的时候被调用),但如果 JS 引擎被备份,它会更高,甚至可能是负数。
参考:https://developer.mozilla.org/en/DOM/window.setTimeout
(见黄色部分)
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)