Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setInterval/setTimeout return value

Two questions:

  1. How is the value returned from setInterval and setTimeout (the ones used to clear the timers) calculated?

  2. Is it possible for both the functions to return the same value during runtime? For example:

    var a = setInterval(fn1, 1000);
    var b = setTimeout(fn2, 1000);

Is it possible for a and b to have the same value?

The first one is more of a for-my-knowledge question, but the second one is more important.

like image 939
aditya Avatar asked Jun 02 '09 15:06

aditya


People also ask

How do I return a value from setTimeout?

To get return value from setTimeout with JavaScript, we can create a promise from the setTimeout code. const x = () => { const promise = new Promise((resolve, reject) => { window. setTimeout(() => { resolve("done!"); }); }); return promise; }; const y = async () => { const result = await x(); console. log(result); };

What is the return value of setInterval?

The returned intervalID is a numeric, non-zero value which identifies the timer created by the call to setInterval() ; this value can be passed to clearInterval() to cancel the interval.

Which is better setTimeout or setInterval?

setTimeout allows us to run a function once after the interval of time. setInterval allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.

How do you use setInterval and setTimeout?

attempt follows: a = setInterval(function() { setTimeout(function(){print 'one'},0); setTimeout(function(){print 'two'},2500); },5000);


2 Answers

Returns a value which can be used to cancel the timer. So, it would seem unlikely that they return the same value (unless they are reusing values and one of the timers has already been cancelled)

Mozilla states it's DOM level 0, but not part of the specification. (look at the bottom of the page)

I've got an even better reference:

Nabble says:

SetTimeout and setInterval are from the original Javascript specification, pre-ECMA. That specification is not officially standardized anywhere, but it is supported by all web browsers and most implementations of the Javascript language. (Including ActionScript.)

The pre-ECMA specs are often known as the "DOM-0" APIs. Since they have never been standardized before, it makes sense for HTML5 to finally spec the non-deprecated APIs in an attempt to provide a consistent environment across browsers. Especially when recent events have proven that there are companies who like to implement the letter of the standard, but not the spirit.

Read the original spec here, or from Sun (who was an early endorser of JavaScript).

like image 166
cgp Avatar answered Sep 21 '22 21:09

cgp


Tested this under Opera 9, Safari 3, Firefox 3 and IE 7.

All returned integer values, starting at 1 and then incrementing by 1 for each call to setTimeOut() and setInterval(). However, I noticed that the browsers started the counters and handled them differently:

  • IE started with a (seemingly) random 6-digit number, but subsequent calls to either function incremented this number. After closing and reopening IE I found that the starting number appeared to be randomly generated, as it was nowhere near the count from the previous session.
  • Opera maintained a counter for each tab - closing a tab and opening a new one started the counter from 1 in the new tab.
  • In Safari, the count was global - opening a new tab and calling the functions in different tabs seemed to increment a global reference counter.
  • In Firefox, the counter appeared to start at 2, and incremented on each subsequent call to either function. Like Opera, each tab had its own counter value, but they appeared to all start at 2.

Notice though, that in all the scenarios, no two identifiers (at least in the same tab) are the same.

like image 20
Alex Rozanski Avatar answered Sep 20 '22 21:09

Alex Rozanski