Does Javascript have anything similar to VBA's DoEvents? Does Javascript have anything similar to VBA's DoEvents? vba vba

Does Javascript have anything similar to VBA's DoEvents?


If your application really requires long-running JavaScript code, one of the best ways to deal with it is by using JavaScript web workers. JavaScript code normally runs on the foreground thread, but by creating a web worker you can effectively keep a long-running process on a background thread, and your UI thread will be free to respond to user input.

As an example, you create a new worker like this:

var myWorker = new Worker("worker.js");

You can then post messages to it from the js in the main page like this:

myWorker.postMessage([first.value,second.value]);console.log('Message posted to worker');

And respond to the messages in worker.js like this:

onmessage = function(e) {  console.log('Message received from main script');  var workerResult = 'Result: ' + (e.data[0] * e.data[1]);  console.log('Posting message back to main script');  postMessage(workerResult);}


With the introduction of generators in ES6, you can write a helper method that uses yield to emulate DoEvents without much syntactic overhead:

doEventsOnYield(function*() {    ... synchronous stuff ...    yield;  // Pump the event loop. DoEvents() equivalent.    ... synchronous stuff ...});

Here's the helper method, which also exposes the completion/failure of the function as a Promise:

function doEventsOnYield(generator) {    return new Promise((resolve, reject) => {        let g = generator();        let advance = () => {            try {                let r = g.next();                if (r.done) resolve();            } catch (ex) {                reject(ex);            }            setTimeout(advance, 0);        };        advance();    });}

Note that, at this time, you probably need to run this through an ES6-to-ES5 transpiler for it to run on common browsers.


You can use the setTimeout:

setTimeout(function() { }, 3600);

3600 it's the time in milliseconds:

http://www.w3schools.com/jsref/met_win_settimeout.asp