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
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,
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
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
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
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
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
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
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
Recently encountered some problems in the work, is roughly about the JS implementation problem. Some strange bugs were caused by the failure to understand the order of execution. So here's some knowledge about asynchronous execution (tip of 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 following code, how long will the ' end ' pop up? Why?
The code is as follows
Copy Code
var t = true;settimeout (function () {t = false;}, 1000);while (t) {}Alert (' End ');
This is an implementation that
SetTimeout is often used in JavaScript to delay the execution of a function, for example:
SetTimeout (function () {alert (& quot; Hello World & quot;) ;}, 1000)
The alert window will pop up one second after this sentence is executed. Let's look at
The code is as follows
Copy Code
ShowTime ();function ShowTime (){var today = new Date ();Alert ("The time is:" + today.tostring ());SetTimeout ("ShowTime ()", 5000);}
Once this function is called, the time is displayed
How to pass parameters to SetTimeout, setintervallook at the following code:
var str = ' AAA ';
var num = 2;
function Auto (num) {
alert (num);
}
SetTimeout (' Auto (num) ', 4000);
This is a good way to write, but as it says
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
SetTimeout Method
Evaluates an expression after a specified number of milliseconds have elapsed. Syntax var retval = window.settimeout (expression, msec, language); Parameters expression [in]
Type: Variant
Variant that specifies the function pointer
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 (
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,
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
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.