setTimeout 在 Windows 脚本(jscript)中不起作用

发布于 2024-08-20 09:13:53 字数 280 浏览 5 评论 0原文

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

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 技术交流群。

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

发布评论

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

评论(4

我的鱼塘能养鲲 2024-08-27 09:13:53

听起来您正在非基于浏览器的脚本(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, use WScript.Sleep to suspend your script briefly, with which you can achieve a similar effect. Also, alert is not a WSH function; you may want WScript.Echo. More on the WSH reference on MSDN.

ㄟ。诗瑗 2024-08-27 09:13:53

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

如果您想暂停脚本执行,可以使用 Sleep< WScript 对象的 /a> 方法。

setTimeout is a method of the window 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.

初心 2024-08-27 09:13:53

我需要 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();

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.

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();
断爱 2024-08-27 09:13:53

对于任何正在寻找在独立脚本(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.

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