Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

javascript - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...