Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7429935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:03:41+00:00 2026-05-29T09:03:41+00:00

Quite often see in JavaScript libraries code like this: setTimeout(function() { … }, 0);

  • 0

Quite often see in JavaScript libraries code like this:

setTimeout(function() {
    ...
}, 0);

I would like to know why use such a wrapper code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:03:42+00:00Added an answer on May 29, 2026 at 9:03 am

    Very simplified:

    Browsers are single threaded and this single thread (The UI thread) is shared between the rendering engine and the js engine.

    If the thing you want to do takes a lot of time (we talking cycles here but still) it could halt (pause) the rendering (flow and paint).

    In browsers there also exists "The bucket" where all events are first put in wait for the UI thread to be done with whatever it’s doing. As soon as the thread is done, it looks in the bucket and picks the task first in line.

    Using setTimeout you create a new task in the bucket after the delay and let the thread deal with it as soon as it’s available for more work.

    A story:

    After 0 ms delay create a new task of the function
    and put it in the bucket. At that exact moment the UI thread is busy
    doing something else, and there is another tasks in the bucket
    already. After 6ms the thread is available and gets the task in front
    of yours, good, you’re next. But what? That was one huge thing! It has
    been like foreeeeeever (30ms)!!

    At last, now the thread is done with that and comes and gets your
    task.

    Most browsers have a minimum delay that is more than 0 so putting 0 as delay means: Put this task in the basket ASAP. But telling the UA to put it in the bucket ASAP is no guarantee it will execute at that moment. The bucket is like the post office, it could be that there is a long queue of other tasks. Post offices are also single threaded with only one person helping all the task… sorry customers with their tasks. Your task has to get in the line as everyone else.

    If the browser doesn’t implement its own ticker, it uses the tick cycles of the OS. Older browsers had minimum delays between 10-15ms. HTML5 specifies that if delay is less then 4ms the UA should increase it to 4ms. This is said to be consistent across browsers released in 2010 and onward.

    See How JavaScript Timers Work by John Resig for more detail.

    Edit: Also see What the heck is the event loop anyway? by Philip Roberts from JSConf EU 2014. This is mandatory viewing for all people touching front-end code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear experts, I am quite new to javascript and I often see coders use
I've see this sort of thing in Java code quite often... try { fileStream.close();
Quite often in ANSI C code I can see parenthesis sorrounding a single return
I quite often see the following naming convention used in java code. class SomeClass
Quite often I see source code where language's keyword are replaced with full type
How often do we see stuff like Send this page to a friend on
In this linked image , I see the button on the right quite often
While learning Java I stumble upon this error quite often. It goes like this:
I quite often see JavaScript with variables that start with a dollar sign. When/why
I quite often see on the Internet various complaints that other peoples examples of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.