【问题标题】:setTimeout not working in windows script (jscript)setTimeout 在 Windows 脚本 (jscript) 中不起作用
【发布时间】:2010-02-04 09:13:33
【问题描述】:

当我尝试在我的程序中运行以下代码时

setTimeout("alert('moo')", 1000);

我收到以下错误

Error: Object expected
Code: 800A138F
Source: Microsoft JScript runtime error

为什么?我调用了错误的函数吗?我想做的是延迟后续函数的执行。

【问题讨论】:

  • 我们需要看到比这更多的代码。

标签: javascript wsh


【解决方案1】:

听起来您在非基于浏览器的脚本(Windows 脚本宿主或类似脚本)中使用了setTimeout。你不能那样做。但是,您可以使用WScript.Sleep 暂时挂起您的脚本,这样您可以获得类似的效果。此外,alert 不是 WSH 函数;你可能想要WScript.Echo。更多关于WSH reference on MSDN

【讨论】:

  • 大声笑不要再这样了 ;-) 我刚刚将 Sleep 方法与 MSDN 链接一起编辑到我的答案中。
  • @Andy:哈哈!回报! (现在 Pekka 在哪​​里?)
【解决方案2】:

setTimeout 是 Web 浏览器提供的 window 对象的方法。它不适用于在 Windows Script Host 上运行的脚本。这些脚本从开始到结束只有一个执行线程,并且没有延迟计时器。

如果要暂停脚本执行,可以使用WScript 对象的Sleep 方法。

【讨论】:

    【解决方案3】:

    我需要 WSH 在使用 setTimeout 的浏览器中表现得像类似的代码,所以这就是我想出的。

    只需让您的单线程执行队列中的所有内容。您可以继续添加到队列中。只有当队列中没有任何函数时,程序才会终止。

    它不支持 eval 字符串,只支持函数。

    function main() {
      Test.before();
      _setTimeout(Test.timeout1, 1000);
      _setTimeout(Test.timeout2, 2000);
      _setTimeout(Test.timeout3, 500);
      _setTimeout(Test.error, 2001);
      Test.after();
    }
    
    var Test = function() {
      var ld = "---- ";
      var rd = " ----";
      return {
        before : function() {
          log(ld + "Before" + rd);
        },
        after : function() {
          log(ld + "After" + rd);
        },
        timeout1 : function() {
          log(ld + "Timeout1" + rd);
        },
        timeout2 : function() {
          log(ld + "Timeout2" + rd);
        },
        timeout3 : function() {
          log(ld + "Timeout3" + rd);
        },
        error : function() {
          log(ld + "error" + rd);
          errorFunc();
        }
      };
    }();
    
    var FuncQueue = function() {
      var funcQueue = [];
      function FuncItem(name, func, waitTil) {
        this.name = name;
        this.func = func;
        this.waitTil = waitTil;
      }
      return {
        add : function(func, name, waitTil) {
          funcQueue.push(new FuncItem(name, func, waitTil));
        },
        run : function() {
          while (funcQueue.length > 0) {
            var now = new Date().valueOf();
            for ( var i = 0; i < funcQueue.length; i++) {
              var item = funcQueue[i];
              if (item.waitTil > now) {
                continue;
              } else {
                funcQueue.splice(i, 1);
              }
              log("Executing: " + item.name);
              try {
                item.func();
              } catch (e) {
                log("Unexpected error occured");
              }
              log("Completed executing: " + item.name);
              break;
            }
            if (funcQueue.length > 0 && i > 0) {
              if (typeof (WScript) != "undefined") {
                WScript.Sleep(50);
              }
            }
          }
          log("Exhausted function queue");
        }
      }
    }();
    
    function _setTimeout(func, delayMs) {
      var retval = undefined;
      if (typeof (setTimeout) != "undefined") {
        retval = setTimeout(func, delayMs); // use the real thing if available
      } else {
        FuncQueue.add(func, "setTimeout", new Date().valueOf() + delayMs);
      }
      return retval;
    }
    
    var log = function() {
      function ms() {
        if (!ms.start) {
          ms.start = new Date().valueOf();
        }
        return new Date().valueOf() - ms.start; // report ms since first call to function
      }
      function pad(s, n) {
        s += "";
        var filler = "     ";
        if (s.length < n) {
          return filler.substr(0, n - s.length) + s;
        }
        return s;
      }
      return function(s) {
        if (typeof (WScript) != "undefined") {
          WScript.StdOut.WriteLine(pad(ms(), 6) + " " + s);
        } else {
          // find a different method
        }
      }
    }();
    
    FuncQueue.add(main, "main");
    FuncQueue.run();
    

    【讨论】:

      【解决方案4】:

      对于正在搜索警报功能以在独立脚本(Windows 脚本宿主环境)中工作的任何人,我建议查看 jPaq's 警报功能,该功能已记录在 here 和可下载的 here 中。我确实发现这个新库对我的独立脚本很有帮助。

      【讨论】:

        猜你喜欢
        • 2017-08-30
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 2016-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-20
        相关资源
        最近更新 更多