Read the JavaScript Advanced Programming (third Edition) today, and found that settimeout is better than setinterval, and feels that it does. Usually use setinterval more points, now or turn the idea. I learned it again. The notes are as follows:-------------------------------------------------------------------------------------------------------------------- -----------------------settimeout contains two
1,setinterval ()The SetInterval () method can invoke a function or expression according to a specified period, and he will keep calling the function until the call to the Clearinterval () method or window is closed. The ID value returned by setinterval (), which can be used as a parameter to the Clearinterval ().Grammar:var id = setinterval (code,millisec);Clearinterval (ID);Instance:1 HTML>2 Body>3 4 inputtype= "text"ID= "Clock"size= "+" />5 Scriptlanguage=javascript>6 var int=Self.setinterval
Several examples illustrate how to run setTimeout in JS.
Example: 1
Function test () {var a = 1; setTimeout (function () {alert (a); a = 5 ;}, 1000) ;}test (); alert (0 ); // The first two settimeouts are delayed. Therefore, execute thisResult: 0 is displayed, and 1 is displayed.
Example 2
function test() { var a = 1; setTimeout(function() { alert(a);
When using JScript, we sometimes need to execute a method at intervals, for example, to generate webpage UI animation effects. This is often the setInterval or setTimeout method, but since these two methods are the Timer threads simulated by the Script Host, our method through which we call cannot pass parameters for them.Our common application scenarios are:Copy codeThe Code is as follows:Window. setTimeout
Application Examples of the js setTimeout Function
The window. setTimeout method is used to delay the execution of a function (method. However, it is defective when calling the specified method and passing parameters.
Infinite Loop
The Code is as follows:
The setTimeout function can pass parameters/values.
The
SetTimeout are often used to delay the running of a function, using the method ofSetTimeout (function () {...}, timeout);Sometimes settimeout (function...,0) is used for asynchronous processing, for examplefunction f () {...//Get readySetTimeout (function () {....//Do something}, 0);return ...;}function f is returned before the function processor set by settimeout
In fact, settimeout and setinterval have the same syntax. They all have two parameters, one is the code string that will be executed, and one is the time interval in milliseconds, and the code will be executed after that time period. But there's a difference between these two functions.
In the latest write code, see someone in the project to use SetTimeout (fun,0), so want to sum up. Personal understanding
The difference is that setinterval executes the code once every other specified time period, with repeatability. and settimeout only executes once after the call.
The following is a deep understanding of two functions through the establishment of functions and the automatic deletion of letters;
1. The establishment of the function
The establishment of settimeout:
Copy Code code as follows:
Copy Code code as follows:
settimeout (function (obj) {
alert (OBJ.A);
}, Watts, {a:1});
The third argument is passed, and the third parameter is passed as the parameter obj of the callback function. 1 is popped up in non ie browsers. This has the advantage of resolving the execution context of the callback function, such as a method to invoke an object, which can be passed through parameters.
Copy Code code as follows:
The difference between settimeout and setinterval under JS
A lot of people think the two methods are similar, but, in fact, they are very far away.Because settimeout (expression, delay time) in the execution, is after the load delay specified time, to execute an expression, remember, the number of times is aThe setinterval (expression, interaction time) is not the same, and it executes an expression once a
The two htmldom elements can be interpreted as a function that is timed to execute so that we can run the specified code again at the specified time.
The difference between the two is one settimeout only executes once, setinterval Non-stop execution: Look at the following two:
SetTimeout
The settimeout () method is used to call a function or evaluate an expres
This article explained the use of JS in settimeout (), share for everyone to reference, the specific content as follows
Effect Chart:
Specific code:
Here's a specific usage:
1. Parametercode (required): The JavaScript code string to execute after the function to invoke.Millisec (required): The number of milliseconds to wait before executing code. Tip: settimeout () executes only one code
SetTimeout and setinterval have the same syntax. They all have two parameters, one is the code string that will be executed, and one is the time interval in milliseconds, and the code will be executed after that time period.
However, these two functions are different, setinterval after the execution of the code, after the fixed time interval, it will also automatically repeat the code, and settimeout only o
This is the second chapter of the JavaScript framework series. In this chapter, I plan to explain how asynchronous code is executed in a browser. You will understand the differences between the timer and the event loop, such as setTimeout and Promises. This series is about an open-source client framework called NX. In this series, I mainly explain the main difficulties that the framework has to overcome. This is the second chapter of the JavaScript fr
Recently in the Reading JS DOM programming art, in the tenth chapter of the animation there is a settimeout function in the example of a lot of quotes, study a good freshman will only see, comprehensive online interpretation of the great God and their own understanding, the principle is this:First look at the program source code:1 functionmoveelement (elementid,final_x,final_y,interval) {2 if(!document.getelementbyid)return false;3 if(!documen
SetInterval () Definition and usage
The setInterval () method can execute functions or expressions according to the specified period (in milliseconds. This method will repeatedly call the function until it is explicitly stopped using clearInterval () or the window is closed. The parameter of the clearInterval () function is the ID value returned by setInterval.
Syntax
SetInterval (code, millisec [, "lang"])Code required. The function to be called or the code string to be executed.Millisec is req
This article mainly introduces the things you need to pay attention to when using the closure feature of setTimeout (0) in Javascript. If you need it, you can refer to setTimeout which is often used to delay the execution of a function. Its usage is as follows:
The Code is as follows:
SetTimeout (function (){...}, Timeout );
Sometimes
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 is not the case. The actual situation is that javascript runs in the browser's javascript engine in a single thread, the functions of setTimeout and setInterval are to insert the code you want to execute in
Whether it is window. setTimeout or window. setInterval: parameters are not allowed when the function name is used as the call handle. The solution is to encapsulate the function based on the function, whether it is window. setTimeout or window. setInterval: parameters are not allowed when the function name is used as the call handle. If you want to pass in parameters, custom parameters, or event parameters
2Differences between timer methods...
If multiple Callbacks are expected A Method Performance Considerations ApplicableSetinterval
Out Stability Callback interval considerations ApplicableSetTimeoutMethod
For example
VaR timer;
Function f (){
If (Condition) Clearinterval (timer)
//NoteCleartimeout clearintervalCan be clearedSetTimeoutAndSetintervalTimestamp object generated
SetTimeout (F,Interval)
}
UseSetTimeoutPlace the statement
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.