First look at the use of the timer
1. SetInterval (code,millisec[, "Lang"]) the SetInterval () method can call a function or a calculation expression in the specified period (in milliseconds).
Parameters
Description
Code
Required, the function to invoke or the code string to execute.
Millisec
The time interval, in milliseconds, that is required to periodically execute or call code.
The 2.setTimeout
If you want to execute a function asynchronously, the first way we think of it is settimeoutFor example: settimeout (function (/* 1s after doing something * * *) {},1000}
So what if you're going to execute a function asynchronously, most quickly?Whether it will be:
settimeout function (/* do something as soon as possible) {},0}
Unfortunately, the minimum execution time interval for the
Today, you are experiencing a problem with a timed operation in your project. JavaScript provides two methods for timed operations:
The code is as follows
Copy Code
1, Timeid = settimeout ("function ();", delaytime);
Timeid is the ID of the timer (used later),
The Delaytime unit is 1 per thousand seconds, millisecond.
The code is as follows
Copy Code
2, Timeid = SetInterval ("functio
This function is compatible with ie and firefox. In addition, clearSetTimeOut and clearInterval can be used for clearing. Compared with the original setTimeout, setInterval is much more convenient and the parameter can be an object. /*************************************** ***************
//
// Function: Modify window. setTimeout to pass Parameters and object parameters.
// Usage: window.
Solve the problem of using window. setTimeout () amp; window. setInterval () and passing parameters! 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 para
During the test, it is found that alert will interrupt the timer function of settimeout. After the dialog box is closed, the time of settimeout starts to be timed again, instead of from the break, if you are interested, you can learn that alert will interrupt the timer function of settimeout. After the dialog box is closed, the time of
From a few years ago (more than five years) until now, I have read the setTimeout and setinterval functions in many documents on JavaScript, they always think that their functions are completely the same-that is, how long it takes to execute something, so they always use the setTimeout function and are always in their own
Code I have never used the setinterval function. In fact, why don't I think about it
We all know the role of the setTimeput () function in javascript. It is generally used to handle some continuous tasks. Let's look at an example first:
Maybe many people think the result is: 2 1, but the result is: 1 2. Why? When the delay time is set to 0, should we immediately execute init2? We can think that,The setTimeout () function will apply for a new stack space.But does not belong to the stack space of the current function init (), so init ()
Solutions that do not support jquery selector in setTimeout
In JS, whether it is setTimeout or setInterval, parameters cannot be included when using the function name as the call handle. In many cases, parameters must be included, which requires a solution.
Today, when I wrote a js latency event, I found that the $ (this) of jquery In the setTimeout method did no
SetTimeout (expression, delay time) is used to execute an expression after the specified delay time after loading. The number of times is one.Setinterval (expression, interaction time) is different. It executes the expression at a specified time after loading.
If you specify the number of cycles, you can use setTimeout for each interval.
1: FunctionF (){
2: // Do something here...
3:};
4:
5: For(
Label: I/O Using Ar for data SP Problem C on
Code 1: For (VAR I = 0; I (Function (INDEX ){SetTimeout (function (){Alert (INDEX );},1000 );}) (I );
}
Code 2:
For (VAR I = 0; I (Function (INDEX ){SetTimeout (A (INDEX), 1000 );}) (I );} Function A (PARAM ){Alert (PARAM );}
Why code 1 cannot control the order, and the order of numbers popped out is messy. Code 2 can
The following comments are:
(1) in 2,
How to stop setInterval and setTimeout events
The setInterval and setTimeout methods are often used to execute loop events in js Code. The details of these two methods are not discussed here, this section briefly describes how to stop a loop event.
(1) The setInterval method can call functions or computation expressions according to the specified period (in milliseconds). To stop this method, you can use th
Recently, due to the needs of the project, we need to implement the function of settimeout parameters and search for a lot of resources. Finally found a better way, recorded, and share with you.Either Window.settimeout or Window.setinterval, you cannot take arguments with the function name as the invocation handle.Let's look at a simple JavaScript code:function Show () {3 alert ("Hello World"); 4 } 5 setTimeout
JS in settimeout (timed execution once) and setinterval (interval loop execution) usage is introduced.SetTimeout: Invokes the specified code snippet or function after the specified number of milliseconds;SetTimeout Sample Codefunction timedmsg () { setTimeout(' Alert (' 7 seconds! ') ', ' 5 ')//'popup ' message }settimeout
/** Global functions and methods* 1, SetTimeout, cleartimeout How long after the function is executed (only once)* 2, SetInterval, clearinterval the timer that executes once every few seconds* 3, Unref,ref stop and start timer (for setinterval)*/1, SetTimeout, cleartimeoutA. Specifying how long to execute functions and clear functionsfunction timer () {Console.log ("This was loading after 500S.");}
setInterval.How to stop:The following example calls the clock () function every 50 milliseconds. You can also use a button to stop the clock: [Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]To learn more about setTimeout: 1. html dom setTimeout () methodDefinition and usageThe setTimeout () method is used to ca
During the test, alert will interrupt the settimeout timing function, when the dialog box is closed, settimeout time will start to clock, rather than from the interruption, interested friends can understand
The test found that alert interrupts the settimeout timer, and when the dialog box is closed, settimeout time st
SetInterval () Definition and usage
The SetInterval () method executes a function or expression in the specified period (in milliseconds). The method loops through the function until the function is explicitly stopped by using clearinterval (), or the window is closed. The parameter of the Clearinterval () function is the ID value returned by SetInterval ().
Grammar
SetInterval (code,millisec[, "Lang"])Code required. The function to invoke or the code string to execute.Millisec must. The time
Today in writing a JS delay event found in settimeout this method using JQuery's $ (this) unexpectedly does not work, various tests, and finally concluded that the settimeout does not support jquery selector. So consult the QQ to do jquery development master, immediately put this problem solved, here record.The following is the author's time delay processing of the JS code:
$ ('. Dl_select dt '). Ho
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.