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