JavaScript 等价于 SwingUtilities.invokeLater()
Javascript 中是否有与 SwingUtilities
的 Java invokeLater()
方法等效的方法?
更新 1
那么,零延迟的 setTimeout()
会与 invokeLater()
完全相同吗?
Is there any equivalent of Java's invokeLater()
method of SwingUtilities
in Javascript?
UPDATE 1
So, will setTimeout()
with zero delay do exactly the same as invokeLater()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想异步运行某些内容(稍后),请尝试
setTimeout()
JavaScript 是单线程的。如果您想在事件处理程序之外运行一些耗时(CPU 密集型)的任务,您可以使用上述技术来执行此操作,但它仍然会消耗事件处理线程(导致您的 UI 冻结)。
在浏览器中运行 CPU 密集型任务通常是一个坏主意(网络工作人员可能会改变this),因为它们与事件处理程序共享相同的线程,使它们等待。
另请参阅
If you want to run something asynchronously (later), try
setTimeout()
JavaScript is single-threaded. If you want to run some time consuming (CPU-intensive) task outside of the event handler, you can do this using the technique above, however it will still consume event-handling thread (cause your UI to freeze).
It is generally a bad idea to run CPU-intensive tasks inside a browser (web workers might change this) since they share the same thread as event handlers, making them wait.
See also
尝试了
setTimeout
,它让 UI 给人一种它正在工作的印象,但不知何故花了很长时间。像这样的事情:尝试过
Promise
。结果只是更好更快。所以现在的代码是这样的:Tried
setTimeout
, it made the UI give an impression that it was working but somehow it took a long time. Something like this:Tried
Promise
. outcome was simply better and faster. So the code is now like this: