It is possible that people often encounter situations like the following: for (var i = 1; I ) { setTimeout (function() {alert (i)}, +);}After running, the alert two times the content is the number 3, instead of what you expected first 1 after 2.
Original (http://www.yeeyan.org/articles/view/luosheng/24380)As a beginner, it's important to understand how a timer works in JavaScript. Often their performance behavior is not so intuitive, and this is because they are in a single thread. Let's
Example:1function test () { var a = 1; SetTimeout (function () { alert (a); A = 5; }, 1000);} Test (); alert (0);//The two settimeout in front of it is delayed so perform this firstResult: Pop 0 first, then pop up 1.Example 2function test ()
This time we use settimeout to implement an example of a chronological, sequential printing of numerical values. In fact, in the early days, but also I often make a mistake, or to achieve this ability, it seems that JS more far-fetched, in fact, is
It may even be wrong to interpret two functions that are timed to function as thread-like, thinking that the function that will execute the call in a single time slice seems good and powerful, but it's not. The reality is that JavaScript is running
Let's look at the difference between the settimeout () and the SetInterval () method.
The settimeout () method is used to call a function or evaluate an expression after a specified number of milliseconds.
The SetInterval () method can call a
The code is as follows
Copy Code
settimeout (function () {Alert (' Hello! ');}, 0);SetInterval (callbackfunction, 100);
It is assumed that the greeting method in settimeout is executed immediately, because it is not a
This introduction of setTimeout and setInterval in Javascript objects. For more information, see setTimeout and setInterval in Javascript. The first parameter received by setInterval is a string or function, when setTimeout is used to call the
The setTimeout usage of the timer in javascript is generally as follows. After beginrotate is called, a process of regularly executing rotateloop is started. The following code:Copy codeThe Code is as follows:Var angle = 0;Function rotateloop (){If (
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 periodLet's imagine an unexpected situation, such as the following setinterval
It may even mistakenly understand the two functions that implement scheduled calls as something similar to thread, and think that they will execute the called functions concurrently in a time slice, which seems very good and powerful, however, this
JS manual»settimeout (): Used to call a function or evaluate an expression after a specified number of milliseconds;English explanation»timeout (): timeout, temporary rest, breaks;JS manual»setinterval (): Invokes a function or evaluation expression
One, settimeout this point to the problem
This in settimeout ("This.count ()", 1000) refers to the Window object.
The settimeout of JS is defined as
Copy Code code as follows:
Window.settimeout=function (Vcode, Imilliseconds [,
Thoreau said: "I go to the forest, because I hope to live hard, only to face the purest truth of life, to see if I understand its true meaning, to avoid the time of death, only to find that they have not lived at all ..."
Thoreau is not a program
First JS delay loading
Copy Code code as follows:
In general, the use of settimeout to achieve
Second JS finally loaded
Insert the following code where you want to insert JS:
Copy Code code as follows:
settimeout example
>show an alert box after 2 secondsCancelTips for using settimeout
(1) The call function pass parameter in IE to the settimeoutThe above analysis shows that IE is not supported in the settimeout to the called function
The use of timer settimeout in JavaScript is generally as follows: After calling Beginrotate, you enter a process that executes the rotateloop in a timed, following code.
Copy Code code as follows:
var angle = 0;
function
Currently this settimeout can be well compatible with ie6,7,8,9 as well as Google browser Chrome, Firefox firefox, Apple browser Safari,opera.
SetTimeout is a very good function, Site page front-end engineers often use it for a few seconds after
Original: JavaScript Tour-terminal: the confused settimeout Sometimes the end is not very good, but at least it is a kind of ending, this series of the last article settimeout, this is a confusing function, but also I have been in the slot JSReason,
In JS, whether it is settimeout or setinterval, when using the function name as the call handle can not take parameters, and in many cases must be with parameters, this need to solve the method.
Today in writing a JS delay event found in settimeout
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.