简体   繁体   English

是否有任何情况下setTimeout(..,0)优于requestAnimationFrame()?

[英]Is there any case where setTimeout(.., 0) is better than requestAnimationFrame()?

I've been using setTimeout(function(){ ... }, 0) as a way to delay a function call by one tick. 我一直在使用setTimeout(function(){ ... }, 0)作为将函数调用延迟一个tick的方法。

Normally I've been using this method for when I am trying to directly manipulate the event loop to make sure things execute in certain order, and in most cases it has to do with UI. 通常情况下,当我试图直接操作事件循环以确保事物以特定顺序执行时,我一直在使用此方法,并且在大多数情况下,它与UI有关。

And sometimes I could sense that "tick", especially when I'm using this to run some 3rd party JS library on an element. 有时我会感觉到“滴答”,尤其是当我使用它来运行元素上的第三方JS库时。

But I recently discovered requestAnimationFrame . 但我最近发现了requestAnimationFrame This pretty much achieve the same thing, but in more graceful way. 这几乎达到了同样的目的,但是以更优雅的方式。

Now I'm curious, are there any cases where it's more beneficial to use the setTimeout(function(){ ... }, 0) over requestAnimationFrame(function(){ ... }) ? 现在我很好奇,是有它的更有益使用任何情况下setTimeout(function(){ ... }, 0)requestAnimationFrame(function(){ ... })

From https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame 来自https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame

Only red flag I'm seeing is this guy: 我看到的只有红旗是这个家伙:

The callback rate may be reduced to a lower rate when running in background tabs or in hidden iframes in order to improve performance and battery life. 当在后台标签或隐藏的iframe中运行时,回调率可以降低到较低的速率,以便提高性能和电池寿命。

But I believe that only happens when you've got requestAnimationFrame recursively calling itself. 但我相信只有当你有requestAnimationFrame以递归方式调用自身时才会发生。

So if you've got some long running bit of code that uses requestAnimationFrame, it might take longer if the user is not on that tab. 因此,如果您有一些使用requestAnimationFrame的长时间运行的代码,如果用户不在该选项卡上,则可能需要更长时间。 Other than that, you're using a function to do something it was never intended to do, but then setTimeout wasn't designed as a way to skip a tick so... 除此之外,你正在使用一个函数来做一些它从未打算做的事情,但是setTimeout并没有被设计为跳过勾选的方式所以......

As a word of advice: Generally, I've found you can usually avoid the setTimeout trick by properly using promises, etc. But I have found cases, esp. 作为一个忠告:一般来说,我发现你通常可以通过正确使用promises等来避免setTimeout技巧。但我找到了案例,尤其是。 when using Angular 1.x where I couldn't figure out a better solution than to use it. 当使用Angular 1.x时我无法找到比使用它更好的解决方案。

Interesting tidbit. 有趣的花絮。 With a naive performance test, requestAnimationFrame seems to be significantly faster: 通过简单的性能测试,requestAnimationFrame似乎要快得多:

console.time()
for(let i = 0; i < 100000; i++){
  requestAnimationFrame(() => {(5*5*77/3)*88});
}
console.timeEnd()

gets ~80ms (on my pc) 得到~80ms(在我的电脑上)

console.time()
for(let i = 0; i < 100000; i++){
  setTimeout(() => {(5*5*77/3)*88}, 0);
}
console.timeEnd()

gets ~225ms (on my pc) 得到~225ms(在我的电脑上)

They're not the same thing. 他们不是一回事。

A function that calls itself using setTimeout(fn, 0) will run as often as possible. 使用setTimeout(fn, 0)调用自身的函数将尽可能频繁地运行。

A function that calls itself using requestAnimationFrame(fn) will be called once for every frame -- typically 60 times a second. 使用requestAnimationFrame(fn)调用自身的函数将为每个帧调用一次 - 通常为每秒60次。 As the name implies, it's intended for animations, where it doesn't make sense to update the state of the page any more often than it can be displayed. 顾名思义,它适用于动画,在这种情况下,更新页面状态比显示它更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 requestAnimationFrame 比 setInterval 或 setTimeout 更好 - Why is requestAnimationFrame better than setInterval or setTimeout 有什么比 setTimeout 和 requestAnimationFrame 更快的吗? - Is there anything faster than setTimeout and requestAnimationFrame? 比 JavaScript 的 requestAnimationFrame 方法更好的 animation 方法? - Better animation method than requestAnimationFrame for JavaScript? 有没有比JavaScript中的setTimeout更好的东西? - Is there something better than setTimeout in JavaScript? 使用requestAnimationFrame代替setTimeout进行去抖动/节流是否有任何优势 - Is there any advantage to using requestAnimationFrame instead of setTimeout for debounce/throttle 在测试JavaScript时,还有比setTimeout更好的方法来等待asnyc回调吗? - Any better way than setTimeout to wait for asnyc callbacks when testing JavaScript? 用 requestAnimationFrame() 替换 setTimeout() - Replacing setTimeout() with requestAnimationFrame() JavaScript-setTimeout和requestAnimationFrame中的“ this” - JavaScript - 'this' inside setTimeout and requestAnimationFrame 卡在SetInterval,SetTimeOut和Requestanimationframe中 - Stuck with SetInterval ,SetTimeOut and Requestanimationframe 我应该如何并行使用requestAnimationFrame和setTimeout来制作更好的游戏循环? - How should I use requestAnimationFrame and setTimeout in parallel to make a better game loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM