Firefox setTimeout(func,ms)发送默认参数给回调 [英] Firefox setTimeout(func, ms) sending default parameters to callback

查看:155
本文介绍了Firefox setTimeout(func,ms)发送默认参数给回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在Firefox(3.6 in this)中,如果您调用以下代码:

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

它会在控制台上记录一个随机数字。我知道你可以像Firefox一样传递参数:

$ p $ set $

但似乎Firefox自动发送一个值。我认为它是超过请求的通话时间的毫秒数,但我不能确定。 (EG:now()+ 0ms == now(),但是由于它现在不能调用它等待执行队列并返回那个时间的ms数)?如果我把500ms的超时值通常返回0,除非我有一个很长的脚本在后面。



我也知道Firefox会将超时请求限制在10ms,并且将0设置为10ms。如果这个值是一个延迟值(即:它比我们所需的时间长了126ms)是基于我输入的值(0)还是钳制的最小值?

< hr>

下面的一个答案表明这是计时器句柄。以下代码反驳了(?):

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

这将返回两个不同的值。




当然,它会在IE中返回 undefined ,所以我不会写代码,但我很好奇。



(这实际上是导致我正在处理的一些代码中的一个错误,这个错误依赖于调用函数的可选参数,在IE中工作,而不是FF)。 >

解决方案

来自MDC


参数传递给
回调例程,表示在
毫秒中超时的
lateness。



<因为实际延迟可能比在 setTimeout 调用中指定的延迟时间更长,所以如果函数在指定的延迟之后被精确调用,则延迟否则不为零。

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.

这篇关于Firefox setTimeout(func,ms)发送默认参数给回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆