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. There is a basic interview question, how to make reasonable changes to the code so that it returns the desired results?It's actually very simple. In the StackOverflow of the Great God explained, you can ref
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 take a look at three functions for creating and manipulating a timer.
var id = setTimeout(fn, delay);-Initializes a single timer, and the timer will invoke the specified function after a certain dela
function 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 () { var a = 1; SetTimeout (function () { alert (a); A = 5; }, +); alert (a);} Test (); alert (0);Result: pops up 1
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 my fault, haha! Can not understand the strong place JS. Let's go straight to the subject! Note that if you use setinterval to achieve, it must be very simple, this time we are using
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 in a single-threaded way in the browser's JavaScript engine, and settimeout and setinterval just inserts the code you're going to execute into a code queue that the JS engine maintains at a point in time yo
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 function or a calculation expression in the specified period (in milliseconds).
The SetInterval () method keeps calling the function until the clearinterval () is cal
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 figment of the imagination, but rather a JavaScript API document that explicitly defines how many milliseconds the second parameter meaning is, and the callback metho
When we accidentally tested the compatibility of the AJAXRequest browser, we found that the AJAXRequest. update method has problems in IE in some cases. After testing, we found that it is a problem of setTimeout and setInterval.The problem occurs when AJAXRequest is called. in the update method, if the update interval and the number of updates are involved, a problem will occur in IE. The specific manifestation is that function work is performed when
SetTimeout is often used in Javascript to delay the execution of a function, for example:Copy codeThe Code is as follows:SetTimeout (function () {alert ("Hello World") ;},1000 ); The alert window will pop up one second after this sentence is executed. Let's look at this section again:Copy codeThe Code is as follows:Function (){SetTimeout (function () {alert (1)}, 0 );Alert (2 );}A (); Note that the
In javascript, the timer setTimeout cannot call a local variable. You only need to change the first parameter of setTimeout to a function object instead of a string. The usage of the timer setTimeout in javascript is generally as follows, after beginrotate is called, a process of regularly executing rotateloop is started. The following code:
The Code is as foll
SetTimeout is a very good function. Front-end engineers often use it for operations executed in a few seconds. The setTimeout mentioned below can be well compatible with IE6, 7,8, 9. Currently, this setTimeout is compatible with mainstream browsers such as Google, IE6, 7, 8, and 9, Google Chrome, FireFox, Apple Safari, and Opera.
SetTimeOut and setInterval usage in Javascript, and setinterval usage
The setTimeOut and setInterval of Javascript are used to process latency and scheduled tasks. For example, the pop-up advertisement is automatically closed one minute after the webpage is opened; or the page sends an asynchronous request every 10 seconds to obtain the latest data.1. setTimeOut
Let's use examples to illustrate several methods of setTimeout in jQuery. First, we have prepared the DIV and public functions used for testing:
Copy codeThe Code is as follows:
The basic usage of setTimeout in the original javascript is like this.
Copy codeThe Code is as follows: // basic setTimeout usage in original javascript
FunctionfunA (){
Log ('funa ...
SetTimeout at execution time, after the specified time, after loading, to execute an expression, execute onceSetTimeout at execution time, it executes an expression at a specified time, after it has been loaded
1, Basic usage:Execute a piece of code:
code is as follows
copy code
VAR i=0; settimeout ("I+=1;alert (i)", 1000); execute a function: var i=0
JavaScript's settimeout and setinterval functions are widely used to deal with delays and timed tasks, such as opening a Web page for a period of time after a pop-up login box, the page to send asynchronous requests every time to obtain the latest data and so on. But there are differences in their application.
The settimeout () method is used to call a function or evaluate an expression after a specified n
Let's take a look at the following code and guess the results:
var start = new Date;
settimeout (function () {
console.log (' Elapsed time: ' + (new Date-start) + ' milliseconds ');
while (new Date-start
The result:About 1 seconds after output: 1,After about 1 seconds output: 2,Then the output is now: Time Goes by: 2002 millisecondsFinal output: Time Goes by: 2003 milliseconds
Did you guess right?the functions that are deferred by
Let's illustrate several ways to use settimeout in jquery by example, and first prepare the div and common functions for testing:
Copy Code code as follows:
The basic usage of settimeout in native JavaScript is like this.
Copy Code code as follows:
Basic usage of settimeout in original ecological javascript
Function
A few days ago to learn JS, see two very interesting functions, they are settimeout functions and setinterval functions, these two functions can make the Web page rendering very some of the more common Web page effect, such as Picture Carousel, and some very fun effect. Let's take a look at these two functions!
Syntax and application of a settimeout function and setinterval function
1.
No more nonsense to say, directly to everyone paste code.
Refresh the current window every five seconds
settimeout ("Self.location.reload ();", 5000);
JS timed to close the window (IE and FF tested)
//6 seconds after automatically close the current window
settimeout ("Window.opener=null;window.close ()", 6000);
Below to introduce the JavaScript timer use
Using the timer to implement JavaScrip
Learned a long time of JavaScript, found that the very common two functions, is setinterval and settimeout function, the understanding of these two functions, sometimes feel very vague, after many experiments, finally to it has a more in-depth understanding. Definition, SetInterval ()--interval Specifies the number of milliseconds to execute the specified code continuously. SetTimeout, delay two seconds to
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.