2020-07-07 20:35:24 +00:00
|
|
|
/*
|
|
|
|
Windows Scripting Host doesn't provide setTimeout and clearTimeout.
|
|
|
|
This serves as a replacement.
|
|
|
|
|
|
|
|
To use:
|
|
|
|
|
|
|
|
Set up all the deferred actions you like with the usual setTimeout calls
|
|
|
|
setTimeout(function() {
|
|
|
|
// do something
|
|
|
|
}, 200);
|
|
|
|
|
|
|
|
Then call setTimeout.queue.run().
|
|
|
|
Deferred actions can spawn more deferred actions.
|
|
|
|
If the queue ever runs dry, it will stop running and return.
|
|
|
|
Remember, javascript is single-threaded. If you put more
|
|
|
|
functions in the queue, call the run method again.
|
|
|
|
*/
|
|
|
|
|
2020-07-20 18:57:53 +00:00
|
|
|
exports.VERSIONINFO = "Timer Module (timer.js) version 0.1";
|
|
|
|
exports.global = global;
|
|
|
|
exports.require = global.require;
|
2020-07-07 20:35:24 +00:00
|
|
|
|
2020-07-20 18:57:53 +00:00
|
|
|
exports.sleep = function(ms, callback) {
|
2020-07-07 20:35:24 +00:00
|
|
|
WScript.Sleep(ms);
|
|
|
|
if(typeof(callback) == "function") {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-20 18:57:53 +00:00
|
|
|
exports.setTimeout = function(func, delay) {
|
2020-07-07 20:35:24 +00:00
|
|
|
var when = new Date().getTime() + delay;
|
2020-07-20 18:57:53 +00:00
|
|
|
return exports.setTimeout.queue.add(func, when);
|
2020-07-07 20:35:24 +00:00
|
|
|
};
|
|
|
|
|
2020-07-20 18:57:53 +00:00
|
|
|
exports.clearTimeout = function(timer) {
|
|
|
|
exports.setTimeout.queue.del(timer);
|
2020-07-07 20:35:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// A queue object, with methods add, del, run.
|
|
|
|
// Tied to setTimeout to keep it out of the global namespace.
|
|
|
|
|
2020-07-20 18:57:53 +00:00
|
|
|
exports.setTimeout.queue = (function() {
|
2020-07-07 20:35:24 +00:00
|
|
|
var store = [];
|
|
|
|
var nextid = 0;
|
|
|
|
|
|
|
|
return {
|
|
|
|
add: function(func, when) {
|
|
|
|
store.push({
|
|
|
|
id: nextid,
|
|
|
|
func: func,
|
|
|
|
when: when
|
|
|
|
});
|
|
|
|
return nextid++;
|
|
|
|
},
|
|
|
|
|
|
|
|
del: function(id) {
|
|
|
|
for (var i = 0; i < store.length; i++) {
|
|
|
|
if (store[i].id == id) {
|
|
|
|
store.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
run: function() {
|
|
|
|
while (store.length > 0) {
|
|
|
|
var now = new Date().getTime();
|
|
|
|
for (var i = 0; i < store.length; i++) {
|
|
|
|
var item = store[i];
|
|
|
|
|
|
|
|
if (now > item.when) {
|
2020-07-20 18:57:53 +00:00
|
|
|
exports.setTimeout.queue.del(item.id);
|
2020-07-07 20:35:24 +00:00
|
|
|
item.func(); // <---- actually invoke the function here
|
|
|
|
|
|
|
|
// Note: we can't continue looping through the queue here,
|
|
|
|
// because we removed one of the items. The loop limit is
|
|
|
|
// now incorrect. Easiest thing to do is restart the loop.
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We burn a millisecond here to throttle the looping.
|
|
|
|
// Otherwise it will loop on the order of 200,000 times per sec.
|
|
|
|
|
2020-07-20 18:57:53 +00:00
|
|
|
exports.sleep(1);
|
2020-07-07 20:35:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2020-07-20 18:57:53 +00:00
|
|
|
exports.setTimeout.test = function() {
|
2020-07-07 20:35:24 +00:00
|
|
|
console.log('You should see: C,A,D,N,M');
|
|
|
|
|
2020-07-20 18:57:53 +00:00
|
|
|
exports.setTimeout(console.log('A'), 500);
|
2020-07-07 20:35:24 +00:00
|
|
|
var b = setTimeout(console.log('B'), 1220);
|
2020-07-20 18:57:53 +00:00
|
|
|
exports.setTimeout(console.log('C'), 300);
|
|
|
|
exports.setTimeout(console.log('D'), 1000);
|
2020-07-07 20:35:24 +00:00
|
|
|
clearTimeout(b);
|
|
|
|
|
2020-07-20 18:57:53 +00:00
|
|
|
exports.setTimeout(function() {
|
2020-07-07 20:35:24 +00:00
|
|
|
console.log('N');
|
2020-07-20 18:57:53 +00:00
|
|
|
exports.setTimeout(function() {
|
2020-07-07 20:35:24 +00:00
|
|
|
console.log('M');
|
|
|
|
}, 100)
|
|
|
|
}, 1300);
|
|
|
|
|
2020-07-20 18:57:53 +00:00
|
|
|
exports.setTimeout.queue.run();
|
2020-07-07 20:35:24 +00:00
|
|
|
console.log('done');
|
|
|
|
};
|
|
|
|
|
|
|
|
///// setTimeout.test(); // uncomment to run test
|