📜 ⬆️ ⬇️

setImmediate via MessageChannel

This article will briefly describe another method of implementing a “zero” delay setTimeout (callback, 0).
Anyone who is interested - under the cat.

Briefly about the main thing


Standard setTimeout makes the minimum delay of a few milliseconds, in IE 10 even invented setImmediate which allows you to set a "zero" delay. How it works is well written here ie.microsoft.com/testdrive/Performance/setImmediateSorting/Default.html

Sample code that works in Chrome, Safari, Opera
function setZeroTimeout(callback) {
var channel = new MessageChannel();
channel.port1.onmessage = callback;
channel.port2.postMessage('');
}


Virtues


The advantage of this method as opposed to the method described in dbaron.org/log/20100309-faster-timeouts is that it will work in WebWorkers.
')

Minuses


Not all browsers are supported. Firefox is not working. because Does not support MesageChannel and SharedWorker.

The MessageChannel standard is described in the documentation www.whatwg.org/specs/web-apps/current-work/complete/web-messaging.html#channel-messaging

Source: https://habr.com/ru/post/128079/


All Articles