简体   繁体   English

在所有主流浏览器中,GIF动画是否与javascript在同一个线程上运行?

[英]Is it true that GIF animation runs on the same thread as javascript in all mainstream browsers?

I show an animated GIF that runs while my AJAX request is in progress and gets stopped once my script has processed the response. 我展示了一个动画GIF,它在我的AJAX请求正在进行时运行,并在我的脚本处理完响应后停止。

While this works, I notice that while the response from the request is being processed in my script (which contains some fairly heavy updating of the DOM) the animation freezes. 虽然这有效,但我注意到,虽然请求的响应正在我的脚本中处理(其中包含一些相当大量的DOM更新),但动画会冻结。

My research leads me to believe that this is because the animation of the GIF happens on the same thread that javascript is running on - that the browser is truly single-threaded. 我的研究让我相信这是因为GIF的动画发生在javascript运行的同一个线程上 - 浏览器是真正的单线程。 Is this the correct interpretation for all current mainstream browsers (eg Chrome, Firefox, Safari, IE)? 这是所有当前主流浏览器(例如Chrome,Firefox,Safari,IE)的正确解释吗?

Secondly, why is it done this way? 其次,为什么这样做? Is it really not possible that the browser could dedicate a thread to GIF animations so they didn't freeze when a block of javascript was being executed? 是否真的不可能浏览器可以将一个线程专用于GIF动画,因此当执行一个javascript块时它们没有冻结?

Update 更新

This is an interesting page. 是一个有趣的页面。 It talks about using pure CSS3 animations. 它讨论了使用纯CSS3动画。 They still freeze in Firefox though - maybe soon FF will fix this. 他们仍然在Firefox中冻结 - 也许很快FF就会解决这个问题。 Looks like I should be considering CSS for animation rather that using a GIF. 看起来我应该考虑使用CSS来制作动画,而不是使用GIF。

I suppose the problme is (IMHO) that when you update the dom the browser do not repaint the page, so the image freeze. 我想问题是(恕我直言),当您更新dom时,浏览器不会重新绘制页面,因此图像会冻结。

Try to do heavy thing without change dom, the gif should animate (slowly if your cpu is busy) but not freeze. 尝试做一些没有改变dom的重物,gif应该动画(慢慢地,如果你的cpu很忙)但不要冻结。

If it freeze anyway you could explicitly create a new thread for that work (using webworker) but if i remember right you could not change dom form a webworker. 如果它反正冻结你可以明确地为该工作创建一个新线程(使用webworker),但如果我没记错,你就无法改变dom形成一个webworker。

This is probably well known by now, but CSS3 has it's own thread in most modern browsers. 这可能是现在众所周知的,但CSS3在大多数现代浏览器中都有它自己的线程。 You should be able to set a breakpoint in javascript and still see your css animation running. 您应该能够在javascript中设置断点并仍然看到您的CSS动画正在运行。 Check out this link to see how to animate a sprite with pure css: 看看这个链接,看看如何使用纯css动画精灵:

http://blog.teamtreehouse.com/css-sprite-sheet-animations-steps http://blog.teamtreehouse.com/css-sprite-sheet-animations-steps

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM