Interval call setinterval () in JS and timeout call settimeout () related summary

Source: Internet
Author: User
Tags time in milliseconds

Timeout calls need to use the Window.settimeout (code,millisec) method

It takes two parameters: the code to execute and the time in milliseconds (that is, how many milliseconds to wait before executing the code). The first parameter can be a string containing the JS code (just like the string used in the eval () function), or it can be a function. The second parameter represents the number of milliseconds to wait, but the code specified after the event does not necessarily execute .

This is because JS is a single-threaded interpreter, can only execute a piece of code within a certain time, in order to control the code to be executed there is a JS task queue, these tasks will be added to the queue in the order they are executed.

The second parameter of SetTimeout () tells JS to add the current task to the queue for too long. If the queue is empty, the added code is executed immediately, and if not empty, wait until the previous code finishes executing.

Passing strings is not recommended! May result in a performance loss of Setimeout ("alert (' Hellow world! ')", 1000);//recommended as an anonymous function call var Timeoutid = setTimeout (function () {    alert (" Hellow world! ");      },1000); cleartimeout (Timeoutid);

After calling SetTimeout (), the method returns a numeric value Id,id is a unique identifier for the plan execution code and can be canceled by Cleartimeout (ID) to cancel the non-executing timeout call.

SetTimeout () executes code only once. If you want to call more than once, use SetInterval () or let code itself call SetTimeout () again.

The method of intermittent invocation is setinterval (), the first parameter to be accepted is the function to be called or the code string to execute. , the second parameter is a time interval between periodic execution or calling code, in milliseconds.

The call to the SetInterval () method also returns an ID that can be used to cancel intermittent calls at some point in the future. Canceling an intermittent call is much more important than canceling the timeout call because, without intervening, the intermittent call will be performed until the page is unloaded.

var num = 0,    max = ten,    intervalid = Null;function incrementnumber () {    num++;    if (num = max) {        clearinterval (intervalid);       Alert ("Done");}    } Called as function name Intervalid = setinterval (incrementnumber,500);

In a development environment, a real intermittent call is seldom used because the latter call may be started before the end of the previous call (the set cycle time millisec is too short or the function code executes too long), so it is best not to use intermittent calls.

Questions: common problems with setinterval in JavaScript (setinterval the first argument is quoted and not quoted)

function Fun () {    Console.log ("1");}    SetInterval ("Fun ()", 1000);//global scope Normal execution    setinterval (Fun (), 1000);//Call function OK, setinterval call error    setinterval ( fun,1000);  Correct    setinterval (function () {///anonymous function call        console.log ("1");    })

As shown in the example, the first parameter of setinterval can be a function name, an anonymous function, a reference to a function, and other executable code.

SetInterval ("Fun ()", 1000)

This way of quoting can be understood as the executable code on the line eval () to execute the first parameter, that is, the call to the fun method is a matter of course pop-up 1 seconds interval, has been executed.

SetInterval (Fun (), 1000)

Fun () is a direct call to a function, which means that the setinterval has not started a function fun. If the function does not have a return value or if the return value is not an executable function or other code, it is meaningless if the above code simply pops a 1 and then stops.

SetInterval (fun,1000)

At this point, the first parameter of SetInterval is considered a reference to the function name or function of the argument.

I personally think it is preferable to call the anonymous function in the best way.

Interval call setinterval () in JS and timeout call settimeout () related summary

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.