About JavaScript timeout call and intermittent call _ javascript tips-js tutorial

Source: Internet
Author: User
JavaScript is a single-threaded language, but it allows you to schedule code execution at a specific time by setting the timeout value and interval value. The former executes the code after the specified time, while the latter executes the code once at the specified time. Intermittent call

In JavaScript, intermittent calls are very common. setInterval means repeated calls at intervals.

The setInterval () method receives two parameters: the first parameter can be a string or a function, and the second parameter is a number in milliseconds, indicating the duration of the repetition interval.

The parameter is a string

When the first parameter is a string, it is as follows:

setInterval("alert('this is a string.')",1000);

The string here is a piece of JavaScript code. It is the same as the input eval () function parameter. If there are two quotation marks inside and outside, remember that the quotation marks should not be the same.

SetInterval () returns a numeric ID, which is the unique identifier of the code to be executed, so you can use it to cancel repeated operations. The method corresponding to setInterval () to cancel the operation: clearInterval (). Of course, to cancel the repeated operation, clearInterval () must be placed before execution is complete.

For example:

var intervalId=setInterval(...);clearInterval(intervalId);

First, we get the ID and pass the ID to clearInterval (). Because the cancel Operation is immediately followed by setInterval (), you can cancel the operation immediately, just like it didn't happen.

The parameter is a function.

When the passed parameter is a string, it may cause performance loss. In general, the most commonly used parameter is to pass a function to it.

As follows:

var num=0;function increNum(){ num++; if(num>=10){  clearInterval(intervalId);  alert('over'); }}intervalId=setInterval(increNum,500);

This Program sets an increNum function and passes it as a parameter to setInterval (). At the same time, when the operation is repeated for 10 times, the operation is canceled and a warning box is displayed.

Timeout call

The timeout call is similar to the intermittent call. setTimeout () also receives two parameters. The first one can be a string containing JavaScript code, but also a function, the second parameter is that the delay time is the same as that of the setInterval () method.

But here we need to describe one point:

The delay does not mean that the program will be executed after the preset delay.

Why?

Because JavaScript is a single-thread interpreter, you can only execute a piece of code within a certain period of time and cannot execute multiple pieces of code at the same time. Therefore, there is a task queue in JavaScript, the tasks to be executed are arranged in the queue in sequence. The set delay time is used to add the current task to the task queue. If no task is executed, the code added to the task queue will be executed immediately. If there is still a code segment being executed, the newly added task will be executed only after the code segment is executed.

Similarly, setTimeout () also has a return ID. You can also use this value ID to cancel the timeout call. The corresponding cancellation method is clearTimeout ().

Here, we use the timeout call method to repeat the code that is repeatedly executed in the intermittent call:

var num=0;function increNum(){ num++; if(num<=10){  setTimeout(increNum,500); }else{  alert('over'); }}setTimeout(increNum,500);

This program can also perform repeated operations and terminate the operation after 10 times. What is different from the preceding setInterval () is that it does not use the returned value ID.

Because setInterval () is executed repeatedly, a value ID is always returned, so you must keep track of this value ID, and setTimeout () is not executed after execution, therefore, we do not need to trace the returned value ID, which brings us some convenience.

In addition, the latter call may be called before the previous call is completed. This happens when the function execution time is longer than the interval call time, therefore, it is better to use setTimeout () to simulate intermittent calls.

Of course, using setInterval () in a relatively simple program is still not a big problem (it is reasonable to think of a sentence that can exist ~~~~).

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.