Windows Script Host 中可以异步调用 JavaScript 函数吗?
假设您在 Windows Script Host (JScript) 环境中有一个简单的函数:
function say(text) {
WScript.Sleep(5000);
WScript.Echo(text);
}
是否可以异步调用 say()
?
注意:诸如 setInterval()
或 setTimeout
等基于浏览器的方法在 WSH 中不可用。
Imagine you have a simple function in Windows Script Host (JScript) environment:
function say(text) {
WScript.Sleep(5000);
WScript.Echo(text);
}
Is it possible to call say()
asynchronously?
Note: Such browser-based methods as setInterval()
or setTimeout
are not available in WSH.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,Windows 脚本宿主不支持异步调用脚本函数。您必须同时运行两个脚本才能达到此效果:
No, Windows Script Host doesn't support calling script functions asynchronously. You'll have to run two scripts simultaneously to achieve this effect:
据我所知,Windows Script Host 下没有相当于
setTimeout
/setInterval
的功能(令人震惊)。但是,您可能会在另一个答案中找到 这个简单的函数队列这是一个模仿它的有用起点。基本上,这个人所做的(他的名字也是“TJ”,但不是我)是创建一个函数队列,然后将其主循环称为主方法。主循环模拟基于浏览器的实现中的心跳。非常聪明,尽管我会稍微改变一下命名。As far as I know, there is no equivalent to
setTimeout
/setInterval
under Windows Script Host (shockingly). However, you may find this simple function queue in another answer here on SO a useful starting point for emulating it. Basically what the guy did (his name is also "TJ", but it's not me) was create a function queue, and then you call its main loop as your main method. The main loop emulates the heartbeat in browser-based implementations. Quite clever, though I'd change the naming a bit.