Windows Script Host 中可以异步调用 JavaScript 函数吗?

发布于 2024-10-08 04:56:40 字数 296 浏览 6 评论 0原文

假设您在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

属性 2024-10-15 04:56:40

不,Windows 脚本宿主不支持异步调用脚本函数。您必须同时运行两个脚本才能达到此效果:

// [main.js]
var oShell = new ActiveXObject("WScript.Shell");
oShell.Run(WScript.FullName + " say.js Hello");

WScript.Echo("Hello from main");

 

// [say.js]
WScript.Sleep(5000);
WScript.Echo(WScript.Arguments.Item(0));

No, Windows Script Host doesn't support calling script functions asynchronously. You'll have to run two scripts simultaneously to achieve this effect:

// [main.js]
var oShell = new ActiveXObject("WScript.Shell");
oShell.Run(WScript.FullName + " say.js Hello");

WScript.Echo("Hello from main");

 

// [say.js]
WScript.Sleep(5000);
WScript.Echo(WScript.Arguments.Item(0));
蓝眸 2024-10-15 04:56:40

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文