[Original] congratulations on the official acceptance of the Chinese translation of "javascript Secret Garden"

Source: Internet
Author: User

Congratulations, the Chinese translation of "javascript Secret Garden" was officially adopted by the official, you can visit at any time through the official website: http://bonsaiden.github.com/JavaScript-Garden/zh/

As this document is constantly updated, I will update the Chinese translation in time if there is any new update or correction.

This articleArticleThe cause is some netizens mentioned issue: https://github.com/BonsaiDen/JavaScript-Garden/issues/#issue/68

In general, the description of setinterval is not accurate in the original text, but the description on stackoverflow.com is correct. In my learning attitude, I carefully read two descriptions:

Javascript secret garden:

When the execution of the callback function is blocked,SetintervalMore destruction commands will be released. At a very small interval, this will cause the callback function to accumulate.

Stackoverflow.com:

Intervals try to 'catch up' to get back on schedule. But, they don't queue one on top of each other: There can only ever be one execution pending per interval.

X represents an interval firing that couldn't execute or be made pending, so instead was discarded.

The focus of the debate is: if the callback function takes a long time to execute (more than the scheduled time), will those callback functions be accumulated?

Currently, "javascript Secret Garden" means accumulation, while the article in stackoverflow means that some callback tasks that are too late to be executed will be discarded. In particular, the graphic description is very vivid:

 
.*••X••X
[------] [------] [------] [------]

To verify who is right and who is wrong, I wrote a javascriptCode: Http://jsfiddle.net/sanshi/3XLHc/

VaR COUNT = 0, start = new date (), interval; function loop () {var I, time; If (count <= 5) {for (I = 0; I <1000000000; I ++) {}} time = new date ()-start; $ ('# result '). append ("<li> time:" + time + "-count:" + Count + '</LI>'); count ++; If (count> = 15) {clearinterval (interval) ;}} interval = setinterval (loop, 1000 );

Execution result:

    • Time: 2840-count: 0
    • Time: 4668-count: 1
    • Time: 6489-count: 2
    • Time: 8358-count: 3
    • Time: 10180-count: 4
    • Time: 12002-count: 5
    • Time: 12004-count: 6
    • Time: 13004-count: 7
    • Time: 14001-count: 8
    • Time: 15001-count: 9
    • Time: 16002-count: 10
    • Time: 17003-count: 11
    • Time: 18017-count: 12
    • Time: 19017-count: 13
    • Time: 20018-count: 14

It can be seen that when count is equal to 5, the time has passed for 12 seconds. According to the Javascript secret garden, the total number of callback functions has been 12-5 = 7, in the following example, if the callback function is executed for a short period of time, it will be executed continuously. However, this is not the case in actual cases. In the future, the callback interval will be 1 second.

In other words, some setinterval Callbacks are discarded. Therefore, the description in the stackoverflow article is correct.

I will update this change to the Chinese translation later (it may take a week later that I have no network in my hometown recently ...).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.