This article mainly introduces how to use the timer in js. If you need it, please refer to it. I hope to help you. This article mainly introduces how to use the timer in js. For more information, see.
There are two dedicated timer functions in javascrui:
1. Countdown timer: timename = setTimeout ("function ();", delaytime );
2. Cyclic Timer: timename = setInterval ("function ();", delaytime );
The first parameter "function ()" is the action to be executed when the timer is triggered. It can be a function or several functions, separated. For example, to bring up two warning windows, you can replace "function ();"
"Alert ('first warning window! '); Alert ('second warning window! '); ", While the second parameter" delaytime "is the interval. in milliseconds, enter" 5000 ", which indicates 5 seconds.
The countdown timer triggers events after the specified time arrives, while the cyclic timer triggers events repeatedly when the time interval arrives. The difference between the two is that the former only works once, the latter does not stop working.
For example, if you want to automatically jump to another page several seconds after opening a page, you need to use the countdown timer "setTimeout (" function (); ", delaytime )", if you want to set a sentence to appear one by one,
You need to use the cyclic timer "setInterval (" function (); ", delaytime )".
To obtain the focus of a form, document. activeElement. id is used. Use if to determine whether the document. activeElement. id and Form ID are the same.
For example, if ("mid" = document. activeElement. id) {alert () ;}, "mid" is the ID of the form.
Timer:
It is used to execute a program after a specific period of time.
Regular execution in JS, difference between setTimeout and setInterval, and l release method
SetTimeout (Expression, DelayTime): After DelayTime, an Expression is executed. setTimeout is used for a delay period before a certain operation.
SetTimeout ("function", time) sets a timeout object
SetInterval (expression, delayTime). Expression is executed for each DelayTime. It is often used to refresh an expression.
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
Here are two examples.
Example 1. When a form is triggered or loaded, a verbatim string is output.
The Code is as follows:
Untitled document