Talking about settimeout and setinterval

Source: Internet
Author: User
Tags setinterval time interval

In fact, settimeout and setinterval have the same syntax. They all have two parameters, one is the code string that will be executed, and one is the time interval in milliseconds, and the code will be executed after that time period. But there's a difference between these two functions.

In the latest write code, see someone in the project to use SetTimeout (fun,0), so want to sum up. Personal understanding, if there are errors in the place also please point out. Thx

To understand how the JavaScript timer works, first understand that the JavaScript engine is single-threaded. This can be understood as a JavaScript engine is a waiter, it has a service queue, all interface element events, timed trigger callbacks, asynchronous request callbacks are queued in this task queue, waiting to be processed. All tasks are a minimum unit and do not interrupt processing. This gives you an understanding of settimeout (fun,0), which does not represent the immediate execution of the code unless the task queue is empty (in fact, the browsers actually do this in a different way, the newer browsers may actually be 4ms; older versions may be a little longer, 16ms is also possible). settimeout (fun,time) means that the fun callback is added to the task queue after a few hours, which means that the fun will be executed at least a time.

As an example:

?

1

2

3

4

5

6

7

8setTimeout (function () {

Console.log (1);

}, 0);

var tem = 0;

for (var i = 1; i < 1000000; i++) {

tem = i;

};

Console.log (2);

Display results as

The code is as follows:

2

1

That is, when the settimeout is executed, the function callback was added to the task queue, but did not execute immediately, because the JS engine is still busy processing the current JS, and only after this section of code to go to the task list to take a new task, so the result is first show 2 after 1.

The SetInterval (fun, Time) method is to add fun to the queue at certain times, so the question is, what if the fun executes longer than time?

Read a piece of code

?

1

2

3

4

5

6

7

8

9

10

11

12

13var num = 0;

var time = setinterval (function () {

var tem = 0;

for (var i = 1; i < 99999999; i++) {

tem = i;

};

num + +;

Console.log (num);

}, 100);

settimeout (function () {

Clearinterval (time);

}, 1000);

The meaning is to execute a piece of code every 100ms, and then clear the timer in 1s. But what about the results?

Display results as

The code is as follows:

1

2

3

That is to say, in fact, there are no executions to that many times. That is, some intervals are skipped, and there are multiple code execution intervals that may be smaller than expected. It turns out that the timer code will be skipped if the timer code instance is present when the timer is added to the queue.

It's a good idea to quote a picture.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.