是安全的传递setInterval或setTimeout一个分数延迟? [英] Is it safe to pass setInterval or setTimeout a fractional delay?

查看:90
本文介绍了是安全的传递setInterval或setTimeout一个分数延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,由于不准确的浏览器计时器,差异目前是可以忽略的,但是为了知识的缘故:没有任何支持setInterval和setTimeout的浏览器,但要求它们被传递一个整数值作为延迟?



或者,用例子表示:

  setInterval (动画,50/3); 

与此浏览器兼容?

  setInterval(animate,17); 


解决方案


$ b

(正如RobG指出的,我没有提供对DOM / JS桥规则本身的引用,他敦促谨慎。我相信 - 但是没有提到结论性的状态 - ToInteger 接口桥的一部分,这里是 jsfiddle 显示超时被作为一个字符串,一个float和一个整数(与JS类型中的float类型相同)传递,在FF8和IE9中工作正常。)



这是因为 DOM接口只接受整数 延迟 setTimeout / setInterval - yup,这些都是在DOM中定义的,而不是在ECMAScript。延迟值被适当地转换为整数值(并且在这方面,调用[JS-internal] ToInteger 函数,其执行截断然而,示例数字实际上会产生略有不同的结果(虽然可能不是很明显): - )



这是因为 50/3 16.66andsomemore - > 16 )和 17 指定不同的超时。



快乐编码。






* ToInteger 定义为 sign(number)* floor(abs(number)),不包括特殊情况。请参见第5版ECMAScript规范的第9.4节。


I know that the difference would currently be negligible due to inaccurate browser timers, but for the sake of knowledge if nothing else: is there any browser that supports setInterval and setTimeout, but requires that they be passed an integer value as a delay?

Or, rephrased with examples, is this:

setInterval(animate,50/3);

as cross-browser compatible as this?

setInterval(animate,17);

解决方案

It is perfectly safe.

(As RobG points out, I haven't provide a reference to the DOM/JS bridge rules themselves and he urges caution. FWIW, I believe -- but have no reference to conclusively state -- that ToInteger is part of the interface bridge. Here is a jsfiddle showing the timeout being passed as a string, a float, and an integral (same type as float in JS) which works fine in FF8 and IE9. Feedback welcome.)

This is because the DOM interface only accepts integers for the delay in setTimeout/setInterval -- yup, these are defined in the DOM, not in ECMAScript. The delay value is appropriately converted to an integral value first (and in this aspect the [JS-internal] ToInteger function is invoked which performs a truncation*).

However, the example numbers will actually yield slightly different results (although it might not be noticable) :-)

This is because, 50/3 (16.66andsomemore -> 16) and 17 specify different timeouts.

Happy coding.


*ToInteger is defined as sign(number) * floor(abs(number)), excluding special cases. See Section 9.4 of the 5th Edition ECMAScript specification.

这篇关于是安全的传递setInterval或setTimeout一个分数延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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