Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of calling setTimeout with a delay of 1?

In the quickstart guide for the Google Drive API, the following function is called once the client library has loaded:

// Called when the client library is loaded to start the auth flow.
function handleClientLoad() {
    window.setTimeout(checkAuth, 1);
}

What is the purpose of calling setTimeout with a delay of 1 like this instead of just calling checkAuth immediately?

like image 888
Jakob Avatar asked Feb 06 '13 23:02

Jakob


People also ask

What happens when setTimeout is 0?

Invoking setTimeout with a callback, and zero as the second argument will schedule the callback to be run asynchronously, after the shortest possible delay - which will be around 10ms when the tab has focus and the JavaScript thread of execution is not busy.

What will happen if we call setTimeout () with a time of 0 ms?

1 Answer. To explain: If you call setTimeout() with a time of 0 ms, the function you specify is not invoked right away. Instead, it is placed on a queue to be invoked “as soon as possible” after any currently pending event handlers finish running.

How do you wait 1 second in JavaScript?

To delay a function execution in JavaScript by 1 second, wrap a promise execution inside a function and wrap the Promise's resolve() in a setTimeout() as shown below. setTimeout() accepts time in milliseconds, so setTimeout(fn, 1000) tells JavaScript to call fn after 1 second.

What is the default delay in setTimeout?

As specified in the HTML standard, browsers will enforce a minimum timeout of 4 milliseconds once a nested call to setTimeout has been scheduled 5 times.


2 Answers

Javascript has asynchronous I/O (ajax/requests) as well as setTimeout and setInterval,

One use of running setTimeout with 1 milisecond (or 0) would be to tell that code to be run after the synchronous code following it. Here is an example

setTimeout(function(){
     alert("World");
},1); 
alert("Hello");

//alerts "Hello" then "World"

I wanted to keep my answer simple and to the point, if you're interested, there is more details about how setTimeout works in the MDN article about it

like image 124
Benjamin Gruenbaum Avatar answered Feb 01 '23 02:02

Benjamin Gruenbaum


By using setTimeout you allow the page to be interactive before the checkAuth function completes.

Essentially, you are preventing the checkAuth from holding up the page.

As a side note, the minimum delay specified in the HTML5 specification is 5ms, so a wait of 1ms will actually be a wait of 5ms. If it is important for you to reclaim that time, you can achieve the same result with a 0ms delay by using window.postMessage. This was originally designed to handle cross-origin communication, but has a similar effect as setting a timeout with 0ms (which you can't do as browsers only allow 5ms - or 10ms in some older browsers).

Lastly, the timing is not guaranteed. JavaScript runs on a single thread, so when you push something out onto a timer it must wait for an opening in the execution of the rest of the JavaScript before it gets to take its turn on the thread - it doesn't run in parallel.

like image 44
Fenton Avatar answered Feb 01 '23 01:02

Fenton