Detailed analysis of JS timer instance

  • 2020-03-26 21:27:00
  • OfStack

1. Counter timer: timename=setTimeout("function();" , delaytime);
2. Loop 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, with "; "between functions. Separate them. For example, to pop up two warning Windows, type "function();" Switch to
"Alert (' first warning window! '); Alert (' second warning window! ');" ; The second parameter "delaytime" is the interval time, in milliseconds, that is, fill in "5000", represents 5 seconds.
The difference between a countdown timer, which triggers events after the specified time has arrived, and a loop timer, which triggers events repeatedly when the interval has arrived, is that the former only works once, while the latter works continuously.
For example, when you open a page and want to automatically jump to another page within a few seconds, you need to use a backtimer "setTimeout("function();"). Delaytime), and if you want to set a sentence to appear one word at a time,
We need to use the loop timer "setInterval("function();" , delaytime) ".

Access to the focus of the form, then use document. ActiveElement. Id. To judge the document using the if. ActiveElement. To the same form of id and id.
Such as: the if (" mid "= = document. ActiveElement. Id) {alert (); },"mid" is the corresponding ID of the form.

Timer:
Used to specify the execution of a program after a specified period of time.

JS timing execution,setTimeout and setInterval differences, and l unwinding method

SetTimeout (Expression,DelayTime). After DelayTime, an Expression and a setTimeout will be executed for a delay of some time, and then an operation will be performed.
SetTimeout ("function",time) sets a timeout object

SetInterval (expression,delayTime), each delayTime, will execute expression. Often used to refresh expressions.
SetInterval ("function",time) sets a timeout object

SetInterval repeats automatically. SetTimeout does not repeat.

ClearTimeout (object) clears the setTimeout object that has been set
ClearInterval (object) clears the setInterval object that has been set


Related articles: