JS Timeout call settimeout and intermittent call SetInterval instance analysis _javascript skills

Source: Internet
Author: User
Tags setinterval

This article analyzes the usage of JS timeout call settimeout and intermittent call setinterval. Share to everyone for your reference. Specifically as follows:

Today, I read the JavaScript Advanced Program Design (third edition) book, found that settimeout than setinterval better, that is true. Usually use setinterval more points, now or turn the train of thought. I learned it again. The analysis is as follows:

SetTimeout contains two parameters, the first parameter is the code to execute, and the second parameter is time.
The first argument can be a string or a function, but it is recommended that you use a function instead of a string.
Using a string is equivalent to the Eval method. result in performance loss.

Cleartimeout ()

The code for the timeout call is executed in the global scope, so the value of this in the function points to the window object in strict mode and is undefined

Copy Code code as follows:
Setinval

var num = 0;
var max = 10;
var intervalid = null;

function Incrementnumber () {
num++;
if (num = max) {
Clearinterval (Innervalid);
Alert (' Done ');
}
}

Intervalid = SetInterval (Incrementnumber (), 500);

SetTimeout to achieve the same function

var num = 0;
var max = 10;
function IncrementNumber2 () {
num++;
if (num < max) {
SetTimeout (incrementnumber2,500);
}else{
Alert (' Done ');
}
}
SetTimeout (incrementnumber2,500);

The comparison shows that there is no need to trace the timeout call ID when using the timeout call, because once the code is executed, the call stops itself if another timeout call is no longer set.

It is generally assumed that a time-out call is the best model for simulating intermittent calls.

In the development environment, there are few real intermittent calls, because the latter intermittent call may start before the end of the previous gap call.

It is best not to use intermittent calls.

I hope this article will help you with your JavaScript programming.

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.