starts to execute, just like in our life, and then start doing something later. In JavaScript mainly through the timer to achieve such requirements, this article will make a summary of the timer, is a detailed usage summary of settimeout ().I. The difference between setinterval and settimeoutSetIntervalThe SetInterval () method invokes a function or evaluates an expression (in milliseconds) in the specified period.Grammar:SetInterval (function expres
SetTimeout ()To execute an expression or function by delaying the specified time from loading;
Execute once only; use with Window.cleartimeout.
I'm here
Copy Code code as follows:
$ (document). Ready (function () {
Settimout (Test (), 200);
function test ()
{
Alert (1);
}
});
It only executes once and a friend says you can use it.
Copy Code code as follows:
SetInterval ("ShowTime ()", 5000);
fun
I. Explanatory notes
1. Overview
SetTimeout: Call a function after the specified delay time or execute a code fragment
SetInterval: Periodically invokes a function or executes a piece of code.
2. Grammar
SetTimeout:
var Timeoutid = window.settimeout (func, delay, [param1, Param2, ...]);
var timeoutid = window.settimeout (code, delay);
Timeoutid is the numeric ID of the delay operatio
A few days ago just sent a use of settimeout to improve the UI response speed of the article, originally felt that the understanding of the settimeout has been deep enough, but yesterday accidentally heard that SetTimeout actually has a "bug": in settimeout 0 o'clock, Can not be accurate timing, also said that IE will
Regular JavaScript call functions (SetInterval and setTimeout) and javascript call Functions
The setTimeout and setInterval syntaxes are the same. They all have two parameters: one is the code string to be executed, and the other is the interval in milliseconds. after that period, the code will be executed.
However, there are differences between the two functions. After the setInterval code is executed, it
If you want to execute a function asynchronously, the first method we think of will certainly be setTimeout. Here is a brief introduction to make it easier for a friend who needs to execute a function asynchronously, the first method we thought of was setTimeout.
For example: setTimeout (function (/* do something after 1 S */) {}, 1000}
So what if we want to exe
The setTimeout inside the above points to the window; Bind, as the name implies.The bind () method creates a new function, and when the new function is called, its this value is the first parameter passed to bind (), and its arguments are the other parameters of bind () and its original parameters.The last sentence of the above definition is a bit around, let's take a look.Bind () takes an infinite number of arguments, the first parameter is the th
SetTimeout ()SetTimeout () is the method of window, but we all skip the top-level object name of window. This is used to set a time. When the time is reached, a specified method will be executed. Let's take a look at the next simple example. This is an example of no practical use, just to demonstrate the setTimeout () syntax.Definition and usage: the
JavaScript often uses settimeout to postpone execution of a function, such as:
Copy Code code as follows:
settimeout (function () {alert ("Hello World");},1000);
A delay of 1 seconds after execution to this sentence pops up the alert window. So look at this paragraph again:
Copy Code code as follows:
function A () {
settimeout
Precautions for using setTimeout in js, jssettimeout
SetTimeout is frequently used in js. The most common is timer.
SetTimeout usage is simple var r = setTimeout (func, 1000 );
First, it uses the return value to clear setTimeout and clearTimeout (r)
Often see settimeout delay 0ms JavaScript code, feel very confused, is it not a delay of 0ms and no delay is not a reason? Later through the investigation of data and experiments to obtain the following two roles, may also have a role I do not know, hope to know that friends in the comments on the back of the generous point.1, realize the asynchronous JavaScript;JavaScript is normally executed in sequence. However, we may let the statement after the s
Delay Automatic CompletionRequirements: In the text input box, listening for user input, to achieve the function of automatic completion.Disadvantages: Each user input a character, will produce an AJAX request, if the user entered a long string of content, the number of requests are many, in fact, the last time, is the user needs.The code is similar to the example above.
Deferred scrollingRequirements: page ads, users need to scroll to where to follo
SetTimeout
SetTimeout () //-run the code after the specified timeClearTimeout () //-cancel setTimeout ()
Note: setTimeout () and clearTimeout () are both functions of the Window object of html dom.
Usage: setTimeout () is used to call a function or computing expression after
This article mainly introduces parameter passing and calling of setTimeout and setInterval functions in JavaScript, two functions can insert the code to be executed into a code queue maintained by the js engine at a set time point. For more information, see
How to pass parameters to setTimeout and setIntervalSee the following code:
var str = 'aaa'; var num = 2; function auto(num){ alert(num); }
Use of settimeout and setintervalBoth of these methods can be used to implement JavaScript after a fixed time period. But each has its own application Scenario.MethodIn fact, settimeout and setinterval have the same syntax. They all have two parameters, one is the code string to execute, and one is the time interval in milliseconds, and the code executes after that time Period.however, the two functions are
Some days ago when I practiced writing a small example, I used a timer and found that in SetInterval and settimeout, the this in the function points to the Window object, as in the following example:varnum = 0;functionObj () { This. num = 1, This. Getnum =function() {Console.log ( This. Num); }, This. Getnumlater =function() {setTimeout (function() {Console.log ( This. Num); }, 1000) }}varobj =Ne
Both methods can be used to execute JavaScript after a fixed period of time. However, they have different application scenarios. Method
In fact, setTimeout and setinterval have the same syntax. Both of them have two parameters. One is to be executed.CodeString, and a time interval in milliseconds. After that time period, the code will be executed.
However, there are differences between the two functions. After the setinterval code is executed, it a
The SetInterval () method can call a function or a calculation expression in the specified period (in milliseconds).
The Clearinterval () method can cancel the periodic method call.
When the setinterval call completes, it returns a timer ID that can be used in the future for the timer
Access, and if the ID is passed to clearinterval, the execution of the invoked process code can be terminated.
JS setinterval () usage
Countdown instance
code is as follows
copy code
Inadvertently testing Ajaxrequest browser compatibility, found that the Ajaxrequest.update method in some cases in IE have problems, after testing to find settimeout and setinterval problems.
The problem is now when you call the Ajaxrequest.update method, if you have the update interval and the number of updates, then there will be problems under IE, which is the function when the update interval is taken, and the function cannot stop executing after
Recently with SetTimeout, setinterval, because the function to be passed to use this, so in-depth understanding of some!The third argument to the settimeout and setinterval functions was to simply define the language type, which was later supported in non-IE browsers and supported under different browsers.The original settimeout function definition:var Timeoutid
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.