By using JavaScript, code can be executed after a set interval, rather than immediately after the function is called. We call it a timing event. Next we have explained in detail the javascript timer event. It is very easy to use the timing event in javascrui. The two key methods are as follows:
SetTimeout ()
Execute Code at a certain time in the future
ClearTimeout ()
Cancel setTimeout ()
SetTimeout ()
Syntax
The Code is as follows:
Var t = setTimeout ("javascript statement", millisecond)
The setTimeout () method returns a value. In the preceding statement, the value is stored in the variable t. If you want to cancel this setTimeout (), you can use this variable name to specify it.
The first parameter of setTimeout () is a string containing JavaScript statements. This statement may be like "alert ('5 seconds! ') ", Or call a function, such as alertMsg ()".
The second parameter indicates how many milliseconds since the current time the first parameter will be executed.
Tip: 1000 milliseconds equals one second.
When the button in the following example is clicked, a prompt box will pop up in five seconds.
The Code is as follows: