I have a module that starts some background jobs in NodeJS (the jobs are HTTP requests), here is a simplified version of it:
var
util = require('util'),
EE = require('events').EventEmitter,
Winston = require('winston');
var logger = new Winston.Logger({transports: [new Winston.transports.Console()]});
function Bot() {
EE.call(this);
this.model = ['job1','job2','job3'];
// null = never had a job, false = job started, true = job finished
this.jobs = {
'job1': null,
'job2': null,
'job3': null };
var mirror = this;
function start_job(job) {
var t = Math.floor(Math.random() * 10 + 1) * 1000;
logger.info('calling ' + job + ' for t=' + t);
setTimeout(function() {
logger.info('finished ' + job);
mirror.jobs[job] = true;
}, t);
}
this.on('start', function() {
logger.info('Starting');
while (mirror.isRunning()) {
for (var i=0; i<mirror.model.length; i++) {
var job = mirror.model[i];
var oldJob = mirror.jobs[job];
if (oldJob === null || oldJob === true) {
mirror.jobs[job] = false;
start_job(job);
}
}
// mirror.shutdown();
}
logger.info('Shutting down');
});
}
util.inherits(Bot, EE);
Bot.prototype.shutdown = function() {
this.running = false;
};
Bot.prototype.start = function() {
this.running = true;
this.emit('start');
};
Bot.prototype.isRunning = function() {
return this.running;
};
var bot = new Bot();
bot.start();
The problem is that the jobs never actually run. start_job() is called but the anon function in setTimeout() never fires. Here is the output:
info: Starting
info: calling job1 for t=6000
info: calling job2 for t=5000
info: calling job3 for t=9000
The script hangs up at this point, in an infinite loop.
Now, if I stop after the first loop iteration, by uncommenting mirror.shutdown(), the jobs start as expected:
info: Starting
info: calling job1 for t=6000
info: calling job2 for t=9000
info: calling job3 for t=1000
info: Shutting down
info: finished job3
info: finished job1
info: finished job2
But that is no good. I have no idea why this is happening, but I'm new to Node and still struggling with some concepts.
I know of a module called Background.js, but I don't know if this would be applicable here or how stable to is (doesn't seem to be an active project). I could try it but I still want to understand this problem before moving on.
The problem is that node.js is single-threaded. That while loop continues running and hogs the event loop, so the setTimeout doesn't have a chance to fire. Instead of a while loop to start jobs, you'll want to put that on a setInterval (or possibly a recursive setTimeout). For example:
this.on('start', function() {
logger.info('Starting');
for (var i=0; i<mirror.model.length; i++) {
var job = mirror.model[i];
var oldJob = mirror.jobs[job];
if (oldJob === null || oldJob === true) {
mirror.jobs[job] = false;
start_job(job);
}
}
setTimeout(function(){mirror.start()}, 50);
logger.info('Shutting down');
});
Related
Let's say there are random sequences of external actions (e.g. scroll events). I need to handle the first action immediately, then dismiss all actions occurred with intervals less than some given delta, and then handle the next one which should be delayed for that delta. Further actions should be processed in the same manner.
This looks like a combination of debounce-immediate and simple debounce. I prepared a diagram to demonstrate the idea.
What is the best solution/approach here? I wonder if there is some ready-made pattern...
UPDATE
I would like to thank all participants! For the research I created plunker with four five different realizations suggested in answers: https://plnkr.co/N9nAwQ.
const handler = [
processEvent, // normal
debounceNext(processEvent, DELAY), // dhilt
makeRateLimitedEventHandler(DELAY, processEvent), // user650881
debounceWithDelay(processEvent, DELAY, 0), // willem-dhaeseleer
_.debounce(processEvent, DELAY, {leading: true}) // lodash debounce + leading,
debounceish(DELAY, processEvent) //Mikk3lRo
];
A great news was the Lodash has a leading-flag debounce implementation which satisfies the issue (thanks to Willem D'Haeseleer). And here is the cool demo from Mikk3lRo' answer, he also provided some useful synthesis.
I investigated the sources and the results: form just visual point to memory allocation stuff... I didn't find any performance issues, and the views seem okey. So the ultima ratio was the code itself. All sources were converted to ES6 (as you can see in Plunker) for I can compare them fully. I excluded my own try (it is a bit excessive, despite I like how it looks). The timestamp version is very interesting! The postDelay version's nice, though it wasn't a requested feature (so that snippet demo has double delay for two lodash demos).
I decided not to have a lodash dependency (in other way I certainly would use lodash debounce with leading option), so I chose debounceish by Mikk3lRo.
PS I would like to share that little bounty (unfortunately there is no such an option) or even take some more scores from my reputation for it (but not 200, is too much and would be unfair to the winner which would have only 100). I even can't vote twice... Nevermind.
A very simple solution in vanilla JS using a single timer:
function debounceish(delta, fn) {
var timer = null;
return function(e) {
if (timer === null) {
//Do now
fn(e);
//Set timer that does nothing (but is not null until it's done!)
timer = setTimeout(function(){
timer = null;
}, delta);
} else {
//Clear existing timer
clearTimeout(timer);
//Set a new one that actually does something
timer = setTimeout(function(){
fn(e);
//Set timer that does nothing again
timer = setTimeout(function(){
timer = null;
}, delta);
}, delta);
}
};
}
function markEvt(e) {
var elm = document.createElement('div');
elm.style.cssText = 'position:absolute;background:tomato;border-radius:3px;width:6px;height:6px;margin:-3px;';
elm.style.top = e.clientY + 'px';
elm.style.left = e.clientX + 'px';
document.body.appendChild(elm);
}
document.addEventListener('click', debounceish(2000, markEvt));
<p>Click somewhere (2000ms delta) !</p>
Comparing 6 proposals using the same type of visualization:
var methods = {
default: function(delay, fn) {
return fn;
},
dhilt_debounceNext: (delay, cb) => {
let timer = null;
let next = null;
const runTimer = (delay, event) => {
timer = setTimeout(() => {
timer = null;
if(next) {
next(event);
next = null;
runTimer(delay);
}
}, delay);
};
return (event) => {
if(!timer) {
cb(event);
}
else {
next = cb;
clearTimeout(timer);
}
runTimer(delay, event);
}
},
Mikk3lRo_debounceish(delta, fn) {
var timer = null;
return function(e) {
if (timer === null) {
//Do now
fn(e);
//Set timer that does nothing (but is not null until it's done!)
timer = setTimeout(function(){
timer = null;
}, delta);
} else {
//Clear existing timer
clearTimeout(timer);
//Set a new one that actually does something
timer = setTimeout(function(){
fn(e);
//Set timer that does nothing again
timer = setTimeout(function(){
timer = null;
}, delta);
}, delta);
}
};
},
user650881_makeRateLimitedEventHandler: function(delta_ms, processEvent) {
var timeoutId = 0; // valid timeoutId's are positive.
var lastEventTimestamp = 0;
var handler = function (evt) {
// Any untriggered handler will be discarded.
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = 0;
}
var curTime = Date.now();
if (curTime < lastEventTimestamp + delta_ms) {
// within delta of last event, postpone handling
timeoutId = setTimeout(function () {
processEvent(evt);
}, delta_ms);
} else {
// long enough since last event, handle now
processEvent(evt);
}
// Set lastEventTimestamp to time of last event after delta test.
lastEventTimestamp = Date.now();
};
return handler;
},
Willem_DHaeseleer_debounceWithDelay: (delay, func) => {
let postDebounceWait;
let timeOutLeading = false;
const debounced = _.debounce((...args) => {
// wrap the handler so we can add an additional timeout to the debounce invocation
if (timeOutLeading) {
/*
for the first invocation we do not want an additional timeout.
We can know this is the leading invocation because,
we set timeOutLeading immediately to false after invoking the debounced function.
This only works because the debounced leading functionality is synchronous it self.
( aka it does not use a trampoline )
*/
func(...args);
} else {
postDebounceWait = setTimeout(() => {
func(...args)
}, delay);
}
}, delay, {leading: true});
return (...args) => {
// wrap the debounced method it self so we can cancel the post delay timer that was invoked by debounced on each invocation.
timeOutLeading = true;
clearTimeout(postDebounceWait);
debounced(...args);
timeOutLeading = false;
}
},
Willem_DHaeseleer_lodashWithLeading: (delta, cb) => {
return _.debounce(cb, delta * 2, {leading: true});
},
Javier_Rey_selfCancelerEventListener: function (delta, fn) {
return function(ev) {
var time = new Date().getTime();
if (ev.target.time && time - ev.target.time < delta) {return;}
ev.target.time = time;
fn(ev);
};
},
};
var method_count = 0;
var colors = ['grey', 'tomato', 'green', 'blue', 'red', 'orange', 'yellow', 'black'];
function markEvt(method) {
var style = 'position:absolute;border-radius:3px;width:6px;height:6px;margin:-3px;';
style += 'background:' + colors[method_count] + ';';
if (method_count > 0) {
style += 'transform:rotate(' + Math.floor(360 * method_count / (Object.keys(methods).length - 1)) + 'deg) translateY(-8px);';
}
var elm = document.createElement('div');
elm.innerHTML = '<span style="width:.8em;height:.8em;border-radius:.4em;display:inline-block;background:' + colors[method_count] + '"></span> ' + method;
document.body.appendChild(elm);
method_count++;
return function(e) {
elm = document.createElement('div');
elm.style.cssText = style;
elm.style.top = e.clientY + 'px';
elm.style.left = e.clientX + 'px';
document.body.appendChild(elm);
};
}
for (var method in methods) {
document.addEventListener('click', methods[method](2000, markEvt(method)));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Note that I needed to make minor adjustments to some of the methods to get a common interface. Adapting Cully's answer took more effort than I was willing to put in considering the comments suggest it doesn't do what the OP wants anyway.
It should be pretty clear that Javier Rey's approach behaves completely differently from the rest. Dhilt, user650881 and my own methods seem consistent. Both of Willem D'Haeseleer's methods have double the delay (and other subtle differences), but seem to behave consistently too. As far as I understand the double delay is completely intentional, though that is not how I understand the OP.
I would say that Willem D'Haeseleer's lodash method is without a doubt the simplest - if you already use lodash that is. Without external dependencies my method is IMO simplest - but I may be biased on that one ;)
You might track the last event time and only create a timer event when a follow-up check is required.
function makeRateLimitedEventHandler(delta_ms, processEvent) {
var timeoutId = 0; // valid timeoutId's are positive.
var lastEventTimestamp = 0;
var handler = function (evt) {
// Any untriggered handler will be discarded.
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = 0;
}
var curTime = Date.now();
if (curTime < lastEventTimestamp + delta_ms) {
// within delta of last event, postpone handling
timeoutId = setTimeout(function () {
processEvent(evt);
}, delta_ms);
} else {
// long enough since last event, handle now
processEvent(evt);
}
// Set lastEventTimestamp to time of last event after delta test.
lastEventTimestamp = Date.now();
};
return handler;
}
var DELTA_MS = 5000;
var processEvent = function (evt) { console.log('handling event'); };
el.addEventHandler('some-event', makeRateLimitedEventHandler(DELTA_MS, processEvent));
The behavior in your visual is no different then the standard lodash debouncing behavior with the leading option, the only difference is that your only displaying half of the delta instead of the full delta.
Therefore, your solution can be as simple as this.
_.debounce(cb, delta * 2, {leading: true});
https://lodash.com/docs/4.17.4#debounce
If you want the last delay to be longer, you can solve that by wrapping both the debounced method and the handler. That way you can set the timeout in the handler, and cancel it in the debounce wrapper.
You do have to check if the current invocation was the leading one in order to not add the timeout in that case.
It could look like this:
const _ = require('lodash');
const bb = require('bluebird');
function handler(arg) {
console.log(arg, new Date().getSeconds());
}
const debounceWithDelay = (func, delay, postDelay) => {
let postDebounceWait;
let timeOutLeading = false;
const debounced = _.debounce((...args) => {
// wrap the handler so we can add an additional timeout to the debounce invocation
if (timeOutLeading) {
/*
for the first invocation we do not want an additional timeout.
We can know this is the leading invocation because,
we set timeOutLeading immediately to false after invoking the debounced function.
This only works because the debounced leading functionality is synchronous it self.
( aka it does not use a trampoline )
*/
func(...args);
} else {
postDebounceWait = setTimeout(() => {
func(...args)
}, postDelay);
}
}, delay, {leading: true});
return (...args) => {
// wrap the debounced method it self so we can cancel the post delay timer that was invoked by debounced on each invocation.
timeOutLeading = true;
clearTimeout(postDebounceWait);
debounced(...args);
timeOutLeading = false;
}
};
const debounceDelay = debounceWithDelay(handler, 50, 2000);
(async function () {
console.log(new Date().getSeconds());
debounceDelay(1);
debounceDelay(2);
debounceDelay(3);
debounceDelay(4);
await bb.delay(3000);
debounceDelay(5);
await bb.delay(3000);
debounceDelay(6);
debounceDelay(7);
debounceDelay(8);
})();
Runnable script:
Here's something that I think works the way you described. If not, it's at least something to go off of.
// set up the event bus
const start = getMilli()
const bus = createBus()
bus.on('event', e => console.log(`[${getPassage(start)}] [${e}] original bus: saw event`))
const wrappedBus = wrapBus(1600, 'event', bus)
wrappedBus.on('event', e => console.log(`[${getPassage(start)}] [${e}] wrapped bus: saw event`))
wrappedBus.on('skipped', e => console.log(`[${getPassage(start)}] [${e}] skipped by wrapped bus`))
wrappedBus.on('last before interval', e => console.log(`[${getPassage(start)}] [${e}] this was the last event before the end of the interval`))
wrappedBus.on('interval tick', _ => console.log(`[${getPassage(start)}] interval tick`))
// trigger events on the bus every so often
let totalTime = 0
const intervalTime = 300
setInterval(() => {
totalTime += intervalTime
bus.trigger('event', totalTime)
}, intervalTime)
function getMilli() {
return (new Date()).getTime()
}
function getPassage(from) {
return getMilli() - from
}
// creates a simple event bus
function createBus() {
const cbs = {}
return {
on: (label, cb) => {
if(cbs.hasOwnProperty(label)) cbs[label].push(cb)
else cbs[label] = [cb]
},
trigger: (label, e) => {
if(cbs.hasOwnProperty(label)) cbs[label].forEach(f => f(e))
},
}
}
// creates a new bus that should trigger the way you described
function wrapBus(waitInterval, eventLabel, bus) {
const newBus = createBus()
let deliveredFirst = false
let gotIgnoredEvent = false
let lastIgnoredEvent = undefined
setInterval(() => {
// just here so we know when this interval timer is ticking
newBus.trigger('interval tick', null)
// push the last event before the end of this interval
if(gotIgnoredEvent) {
gotIgnoredEvent = false
deliveredFirst = false
newBus.trigger(eventLabel, lastIgnoredEvent)
newBus.trigger('last before interval', lastIgnoredEvent)
}
}, waitInterval)
bus.on(eventLabel, function(e) {
if(!deliveredFirst) {
newBus.trigger(eventLabel, e)
deliveredFirst = true
gotIgnoredEvent = false
}
else {
gotIgnoredEvent = true
lastIgnoredEvent = e
// this is here just to see when the wrapped bus skipped events
newBus.trigger('skipped', e)
}
})
return newBus
}
Here's my try:
const debounceNext = (cb, delay) => {
let timer = null;
let next = null;
const runTimer = (delay, event) => {
timer = setTimeout(() => {
timer = null;
if(next) {
next(event);
next = null;
runTimer(delay);
}
}, delay);
};
return (event) => {
if(!timer) {
cb(event);
}
else {
next = cb;
clearTimeout(timer);
}
runTimer(delay, event);
}
};
const processEvent = (event) => console.log(event);
const debouncedHandler = debounceNext(processEvent, 125);
myElement.addEventListener('scroll', debouncedHandler);
I'm new at jQuery and I really hope I can get help with this problem as it is causing me significant headache.
Here is my code:
$(function() {
var global = '0'
run(); // run function run on page load.
function run(){
var cars = new Array("Saab", "Volvo", "BMW");
var wait = new Array("2000", "5000", "10000");
alert (cars[global]);
WAIT (wait[global]) THEN run function cars AGAIN {
run();
global++;
if (global == 4) {
global = '0';
}
}
}
});
So as you can see: on page load function run() runs, alerting "Saab". THEN the function should wait 2000ms and re-run iself. On the next run function run would alert "Volvo" and wait 5000ms before re-running itself. On the third run it would alert "BWM" and wait 10000ms then resetting the global variable to 0 and doing it all over again.
So basically what I will do is populate the two arrays with different variables and create alerts based on user-defined delays.
Thank you so much in advance!
You mean SetTimeout that is used to run code after x amount of milliseconds (here more).
Here is a working solution:
$(function() {
var global = '0'
run(); // run function run on page load.
function run(){
var cars = new Array("Saab", "Volvo", "BMW");
var wait = new Array("2000", "5000", "10000");
alert (cars[global]);
setTimeout(function(){
global++;
if (global == 3){
global = '0';
}
run();
}, wait[global]);
}
})
Here is JSFiddle to it.
I moved:
global++;
if (global == 3){
global = '0';
}
Before the function call as if the call is before increment, it would call the function with global = 0 again first. If you are unsure what I mean by that, try with JSFiddle.
Just clean a bit the code (for fun), here is my code (same code blocks just rearranged):
$(function() {
var cars = new Array("Saab", "Volvo", "BMW");
var wait = new Array("2000", "5000", "10000");
var global = '0'
run(); // Initial run.
function run(){
if (global == 3){
global = '0';
}
alert (cars[global]);
global++;
setTimeout(function(){
run();
}, wait[global]);
}
})
<script>
var index = 0;
var cars = [];
$( document ).ready(function() {
var obj = {car: "Volvo", timeout: "2000"};
cars.push(obj);
var obj = {car: "Saab", timeout: "5000"};
cars.push(obj);
var obj = {car: "BMW", timeout: "10000"};
cars.push(obj);
alertAndReRun();
});
function alertAndReRun(){
console.log(index + " " + (cars.length-1));
alert(cars[index].car);
index++;
if(index <= (cars.length-1)){
setTimeout(alertAndReRun, cars[index].timeout);
}
}
</script>
You can set a timeout before running the function again with window.setTimeout.
Here is a recursive solution for your problem:
$(function() {
var cars = ["Saab", "Volvo", "BMW"];
var wait = ["2000", "5000", "10000"];
var index = 0;
run();
function run() {
if(cars.length < index + 1 || wait.length < index + 1) {
index = 0;
}
var car = cars[index];
var waitingTime = wait[index];
// do something with `car`
console.log(car);
index++;
window.setTimeout(run, waitingTime);
}
});
This example works as you expected, using setInterval()
$(function() {
var timer = null;
run();
function run(){
var cars = new Array("Saab", "Volvo", "BMW");
console.log(cars[global]);
global++;
if (global == 3) {
global = 0;
}
if (timer !== null) {
clearInterval(timer);
}
timer = setInterval(run, parseInt(wait[global]));
}
});
And here a JSFiddle: Time Variable interval
I am ask to write a java script program that retrieve an api JSON record from and address and through websocket every single minute. The stream continues after 60 seconds. I am expected to return the respective stream retrieve and the stream from the previous retrieve . Below is my code
var obj=
{
seconds : 60,
priv : 0,
prevTick : '' ,
data : ''
}
function countTime()
{
obj.seconds --;
obj.priv ++;
var msg ;
if(obj.priv > 1)
{
obj.priv = 0;
obj.msg = null;
}
if(prop.seconds < 0)
{
msg = sock.open();
obj.msg = obj.msg + ", New Tick : " + msg.msg ;
setTimeout(countTime, 1000);
obj.seconds = 60;
}
}
var sock= new WebSocket('link');
sock.onopen = function(evt) {
ws.send(JSON.stringify({ticks:'string'}));
};
sock.onmessage = function(msg) {
var data = JSON.parse(msg.data);
return 'record update: %o'+ data ;
};
Please what is wrong with my code above ? It does not delay at all. The stream continues irrespective.
How about encapsulating the buffering behavior into a class?
function SocketBuffer(socket, delay, ontick) {
var messages = [], tickInterval;
socket.onmessage = function(msg) {
messages.push( JSON.parse(msg.data) );
};
function tick() {
if (typeof ontick !== "function") return;
ontick( messages.splice(0) );
}
this.pause = function () {
tickInterval = clearInterval(tickInterval);
};
this.run = function () {
if (tickInterval) return;
tickInterval = setInterval(tick, delay * 1000);
tick();
};
this.run();
}
Note that .splice(0) returns all elements from the array and empties the array in the same step.
Usage:
var link = new WebSocket('link');
link.onopen = function (evt) {
this.send( JSON.stringify({ticks:'string'}) );
};
var linkBuf = new SocketBuffer(link, 60, function (newMessages) {
console.log(newMessages);
});
// if needed, you can:
linkBuf.pause();
linkBuf.run();
Try this:
function countTime() {
var interval = 1000; // How long do you have to wait for next round
// setInterval will create infinite loop if it is not asked to terminate with clearInterval
var looper = setInterval(function () {
// Your code here
// Terminate the loop if required
clearInterval(looper);
}, interval);
}
If you use setTimeout() you don't need to count the seconds manually. Furthermore, if you need to perform the task periodically, you'd better use setInterval() as #RyanB said. setTimeout() is useful for tasks that need to be performed only once. You're also using prop.seconds but prop doesn't seem to be defined. Finally, you need to call countTime() somewhere or it will never be executed.
This might work better:
var obj=
{
seconds : 60,
priv : 0,
prevTick : '' ,
data : ''
}
function countTime()
{
obj.seconds --;
obj.priv ++; //I don't understand this, it will always be set to zero 3 lines below
var msg ;
if(obj.priv > 1)
{
obj.priv = 0;
obj.msg = null;
}
msg = sock.open();
obj.msg = obj.msg + ", New Tick : " + msg.msg;
obj.seconds = 60;
//Maybe you should do sock.close() here
}
var sock= new WebSocket('link');
sock.onopen = function(evt) {
ws.send(JSON.stringify({ticks:'string'}));
};
sock.onmessage = function(msg) {
var data = JSON.parse(msg.data);
return 'record update: %o'+ data ;
};
var interval = setInterval(countTime, 1000);
EDIT: finally, when you're done, just do
clearInterval(interval);
to stop the execution.
I have a very expensive task running on a Worker, similar as this
for(var i = 0; i < 1000000000; i++)
//operations using i...
How can I make is so that, in that loop, I can check if a message was received from the Worker owner asking it to stop? I would like to have something like this
for(var i = 0; i < 1000000000; i++)
if(windowDidNotAskToStop())
//operations using i...
Right now I have a onmessage function registered so I can start listen to message coming from the owner, but it is blocked while my loop is running (obviously).
I imagine that the postMessage calls from the owner are queued somewhere, so I would simply have to access that in order to process the calls from inside my loop.
You’ll have to handle the events as usual and set a flag, but make sure to leave time for the event to be received in your loop, probably using setTimeout:
var exitLoop = false;
(function loop(i) {
if (exitLoop || i >= 1000000000) {
return;
}
// …
setTimeout(function () {
loop(i + 1);
}, 0);
})(0);
onmessage = function (e) {
if (e.data === 'stop') {
exitLoop = true;
}
};
Or, as a general utility function:
function asyncIterateRange(start, end, callback) {
var exitLoop = false;
var doneCallbacks = [];
(function loop(i) {
if (exitLoop) {
return;
}
if (i >= end) {
doneCallbacks.forEach(function (callback) {
callback();
});
return;
}
callback(function () {
setTimeout(function () {
loop(i + 1);
}, 0);
});
})(start);
return {
abort: function abort() {
exitLoop = true;
},
done: function addDoneCallback(callback) {
doneCallbacks.push(callback);
}
};
}
Use it like so:
var innerIterator;
var outerIterator = asyncIterateRange(0, 1000000, function outerLoop(next) {
innerIterator = asyncIterateRange(0, 1000, function innerLoop(next) {
// …
next();
}).done(next);
});
// To stop:
if (innerIterator) {
innerIterator.abort();
}
outerIterator.abort();
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.
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.