Set latency in JS:
Setinterval is similar to setTimeout.
SetTimeout is used to perform an operation after a delay period.
SetTimeout ("function", time) sets a timeout object
- Setinterval ("function", time) // sets a timeout object
Setinterval indicates automatic repetition, and setTimeout does not.
Cleartimeout (object) clears the set setTimeout object
Clearinterval (object) clears the set setinterval object
1. What is javas?Timer events
Using javascript can implement code execution in a delayed manner. That is to say, when a function is called, it does not execute some code immediately, but waits for a specified period of time before execution. This is called a timing event.
Ii. javasTimer functions
- SetTimeout () //-run the code after the specified time
- Cleartimeout () //-cancel setTimeout ()
Note:SetTimeout () and cleartimeout () are both functions of the window object of HTML Dom.
Iii. setTimeout
VaR T = setTimeout ("javascript statement", time parameter)
Note: The unit of time parameter is millisecond.
Example: var T = setTimeout ("alert ('3 seconds! ') ", 3000)
If the JS statement carries a variable, the variable must be connected with the + sign, for example:
VaR T = setTimeout ("document. getelementbyid (" + menuid + "). style. Display = 'none'", 3000)
Iv. cleartimeout
Syntax: cleartimeout (variable name of setTimeout)
Example: cleartimeout (t) // where T is the setTimeout variable set earlier
You can use cleartimeout to stop timing at any time.
5. Application Skills
We recommend that you set setTimeout as a function. For example:
- Function delayrun (Code, time ){
- VaR T = setTimeout (Code, time );
- }
In this way, when you need to delay the execution of a code segment, you only need to add this function before the code segment. For example:
Onmouseover = delayrun ("settab (500)
Settab is a custom function. If you do not want to delay the execution of settab in the future, remove the delayrun code in the statement,
Change to onmouseover = settab (0, 0.
This method avoids writing a piece of setTimeout code for every place that requires latency. You only need to call it directly, which is very convenient. It also saves the amount of code.