如何在不使用 setTimeout 的情况下异步运行 javascript 函数?
它是服务器端 Javascript(犀牛引擎),因此 setTimeout 不可用。如何异步运行一个函数?
its a server side Javascript (rhino engine), so setTimeout is not available. how to run a function asynchronously?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
java.util.Timer
和java.util.TimerTask
滚动您自己的设置/清除超时和设置/清除间隔函数:You can use
java.util.Timer
andjava.util.TimerTask
to roll your own set/clear Timeout and set/clear Interval functions:查看 Rhino 示例 页面。基本上,JavaScript 不直接支持线程,但您也许可以使用 Java 线程来实现您正在寻找的功能。
Have a look at the
Multithreaded Script Execution
example on the Rhino Examples page. Basically, JavaScript does not support threading directly, but you may be able to use a Java thread to achieve what you are looking for.另一个版本使用 ScheduledThreadPoolExecutor,兼容使用 Rhino 1.7R4 并由 @Nikita-Beloglazov 提议:
参考:https://gist.github.com/nbeloglazov/9633318
Another version using ScheduledThreadPoolExecutor, compatible with Rhino 1.7R4 and proposed by @Nikita-Beloglazov:
Reference: https://gist.github.com/nbeloglazov/9633318