SetInterval and SetTimeout

Source: Internet
Author: User

Basic usage of settimeout and setinterval we don't talk about it, it's nothing but 1. Specify the delay after calling the function, 2. Call a function with a specified period

Let's imagine an unexpected situation, such as the following setinterval

setInterval(function(){    func(i++);},100)

  

We call the Func function every 100 milliseconds, and if the Func execution time is less than 100 milliseconds, it will be able to execute after the next 100 milliseconds:

But what if the Func execution time is greater than 100 milliseconds and the next Func function is triggered before the execution is complete? (only if you know that JavaScript is single-threaded and does not exist while executing the fatal one). As shown in the answer, the second Func waits in the queue (where the queue refers to event loop, which is mentioned in detail below) until the first function finishes executing

If the execution time of the first function is particularly long, how many func should have been triggered during execution, then all the functions that should be triggered will go into the queue?

no, as long as there is a function in the queue that is being executed, then everything else is ignored. For example, callbacks in the No. 300 and 400 milliseconds are discarded, and once the first function is executed, the second in the queue is executed, even if the function is "obsolete" for a long time.

Also, although you specify a period of 100 milliseconds in the setinterval, it does not guarantee that the interval between the two functions will be 100 milliseconds. in the above case, if the second function in the queue ends at the No. 450 millisecond, at the No. 500 millisecond it will continue to perform the next round of Func, that is, the interval between this is only 50 milliseconds, not the period 100 milliseconds

And what if I want to make sure that every time I run it? Use settimeout, such as the following code:

vari =1  var timer = setTimeout(function(){     alert(i++)     timer = setTimeout(arguments.callee,2000)},2000)

SetInterval's question:

1. Some intervals are skipped

2. The interval between code execution for multiple timers may be smaller than expected

So commonly used settimeout instead of setinterval

SetInterval (fn,time)//↓↓↓↓↓↓↓↓↓↓↓↓settimeout (function () {fn (); SetTimeout (arguments.callee,time);},time);
This creates a new timer every time the function executes, and the second settimeout () call uses Agrument.callee to get a reference to the currently executing function and to set up another new timer. This ensures that there will be no new timer insertions until the execution of the code is completed, and that the next timer code must be at least spaced for a specified period of time to avoid running continuously.
callee
Returns the function object being executed, which is the body of the specified function object.
[function.] Arguments.callee
The optional function parameter is the name of the function object that is currently executing.
Description: The initial value of the Callee property is the Function object that is being executed.
The Callee property is a member of the arguments object that represents a reference to the function object itself, which facilitates the recursion of anonymous functions or ensures the encapsulation of functions, such as the recursion of the following example calculates the sum of 1 to N of the natural number. This property is available only if the related function is executing. It is more important to note that Callee has the length property, which is sometimes used for validation or better. Arguments.length is the actual parameter, and the arguments.callee.length is the length of the formal parameter, which can determine whether the parameter length is consistent with the argument length.

Callee can print its own
function Calleedemo () {
alert (Arguments.callee);
}
Recursive calculation
var sum = function (n) {
if (n <= 0) return 1;
else return n +arguments.callee (n-1)
}
Comparison of general recursive functions:
var sum = function (n) {
if (1==n) return 1;
else return n + sum (n-1);
}
When invoked: Alert (SUM (100));
Where the function contains a reference to the sum itself, the function name is just a variable name, call sum inside the function is equivalent to calling a global variable, does not well reflect the call itself, when using callee will be a better method.

SetInterval and SetTimeout

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.