Skip to content
Advertisement

How to execute setTimeout function immediately after the delay?

I want to execute setTimeout after the delay and not wait for it to be called from the message queue after all the other functions have executed. Please refer the code below:

JavaScript

How to get setTimeout after 2 seconds instead of at the end ?

Advertisement

Answer

I thought I would mention that if those test() calls do not perform DOM operations you could possibly move them to a worker.

Web Workers allow running code on a background thread that doesn’t block your main UI thread. I’ve created a quick example that runs a synchronous task of arbitrary duration on a worker thread. On the main thread setInterval and setTimeout calls are running uninterrupted.

See it in action.

index.js

JavaScript

worker.js

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement