setTimeout 在 Windows 脚本(jscript)中不起作用
当我尝试在程序中运行以下代码时,
setTimeout("alert('moo')", 1000);
出现以下错误
Error: Object expected
Code: 800A138F
Source: Microsoft JScript runtime error
为什么?我调用了错误的函数吗?我想做的是延迟后续函数的执行。
When I try to run the following code in my program
setTimeout("alert('moo')", 1000);
I get the following error
Error: Object expected
Code: 800A138F
Source: Microsoft JScript runtime error
Why? Am I calling the wrong function? What I want to do is delay the execution of the subsequent function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来您正在非基于浏览器的脚本(Windows Script Host 或类似脚本)中使用
setTimeout
。你不能那样做。不过,您可以使用WScript.Sleep
短暂挂起脚本,这样您就可以达到类似的效果。另外,alert
不是 WSH 函数;它是一个 WSH 函数。您可能需要WScript.Echo
。有关 MSDN 上的 WSH 参考的更多信息。It sounds like you're using
setTimeout
in a non-browser-based script (Windows Script Host or similar). You can't do that. You can, however, useWScript.Sleep
to suspend your script briefly, with which you can achieve a similar effect. Also,alert
is not a WSH function; you may wantWScript.Echo
. More on the WSH reference on MSDN.setTimeout
是网络浏览器提供的window
对象的一个方法。它不适用于在 Windows Script Host 上运行的脚本。这些脚本从开始到结束都有一个执行线程,并且没有延迟计时器。如果您想暂停脚本执行,可以使用 Sleep<
WScript
对象的 /a> 方法。setTimeout
is a method of thewindow
object provided by web browsers. It's not available to scripts running on Windows Script Host. Those scripts have a single thread of execution from start to finish and have no delay timers.If you want to pause script execution you can use the Sleep method of the
WScript
object.我需要 WSH 的行为类似于浏览器中使用 setTimeout 的类似代码,所以这就是我的想法。
只需让您的单线程执行队列中的所有内容即可。您可以继续添加到队列中。仅当队列中没有任何函数时,程序才会终止。
它不支持 eval 的字符串,只支持函数。
I needed WSH to behave like similar code in browser that uses setTimeout, so here's what I came up with.
Just have your single thread execute everything in a queue. You can keep adding to the queue. The program will only terminate when no functions are left in the queue.
It doesn't support strings for eval, just functions.
对于任何正在寻找在独立脚本(Windows Script Host 环境)中工作的警报功能的人,我建议查看 jPaq 的< /a> 警报功能记录在此处并可下载此处。我确实发现这个新库对我的独立脚本很有帮助。
For anybody who is searching for the alert function to work in a stand-alone script (Windows Script Host environment), I recommend checking out jPaq's alert function which is documented here and downloadable here. I have definitely found this new library to be helpful for my stand-alone scripts.