JavaScript can be used to implement code execution in a delayed manner. That is to say, when a function is called, it does not execute some code immediately. Instead, it is executed after a specified period of time. This is called a timing event.
I. What is a JavaScript timing event?
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. Functions of JavaScript timing events
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. getelementbyidx_xx_x_x_x_x_x (" + 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.