如何让 window.onbeforeunload 等待动画?
考虑以下 JavaScript 代码,并启动一个动画库(即 scriptaculous)。
window.onbeforeunload = function(event) {
document.body.fade();
}
离开页面是瞬时的,无需等待动画完成。尽管我到处都看到人们说 JS 不支持线程,但这里有些东西正在并行运行,因为似乎一个不会等待另一个。我对线程的看法是否正确,有什么方法可以实现我在这里想做的事情吗?
Consider the following JavaScript code, with an animation library (i.e. scriptaculous) to boot.
window.onbeforeunload = function(event) {
document.body.fade();
}
Leaving the page is instantaneous and doesn't wait for the animation to complete. Even though everywhere I look people say JS doesn't support threads, something is running in parallel here, because it seems the one will not wait for the other. Am I right about the threads, and is there any way to achieve what I'm trying to do here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有点晚了,但我认为给出答案对于未来的 SO 用户来说很有趣。
因此,有多种方法可以将您的代码同步到此,因此您应该添加一个自定义服务员。
我想你也可以尝试用承诺:
A bit late but I think give an answer is interesting for futur SO users.
So there is multiple way to sync your code to this and so you should add a custom waiter.
I guess you could also try with promises: