开发者

setTimeout not working in windows script (jscript)

开发者 https://www.devze.com 2022-12-19 07:26 出处:网络
When I try to run the following code in my program setTimeout(\"alert(\'moo\')\", 1000); I get the following 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 t开发者_JS百科he wrong function? What I want to do is delay the execution of the subsequent function.


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.


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.


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();


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号