In javascript, there are two dedicated functions for timers, namely:
1. Countdown 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 one function or several functions. The functions can be separated by ";". For example, if you want to pop up two warning windows, you can replace "function();" with
"alert('First warning window!'); alert('Second warning window!');"; and The second parameter "delaytime" is the interval time in milliseconds, that is, filling in "5000" means 5 seconds.
The countdown timer triggers an event after the specified time arrives, while the loop timer triggers the event repeatedly when the interval arrives. The difference between the two is that the former only works once, while the latter works continuously.
For example, after you open a page and want to automatically jump to another page every few seconds, you need to use the countdown timer "setTimeout("function();",delaytime)", and if you want to To set a sentence to appear one word at a time,
requires the use of the loop timer "setInterval("function();",delaytime)".
To obtain the focus of the form, document.activeElement.id is used. Use if to determine whether document.activeElement.id and the form's ID are the same.
For example: if ("mid" == document.activeElement.id) {alert();}, "mid" is the ID corresponding to the form.
Timer:
is used to specify a program to be executed after a specific period of time.
Timing execution in JS, the difference between setTimeout and setInterval, and the cancellation method
setTimeout(Expression,DelayTime), after DelayTime, an Expression will be executed. setTimeout is used to delay for a period of time before performing an operation.
setTimeout("function",time) sets a timeout object
setInterval(expression, delayTime), Expression will be executed for each DelayTime. It can often be used to refresh expressions.
setInterval("function", time) sets a timeout object
SetInterval is automatically repeated, and setTimeout will not be repeated.
clearTimeout(object) clears the setTimeout object
clearInterval(object) clears the setInterval object
Just give two examples.
Example 1. When the form is triggered or loaded, output the string verbatim
Example 2. When the focus is on the input box, check the input box information regularly, and do not perform the checking action when the focus is not on.
< title>Untitled document
Example 3. The following is the simplest example. A warning window pops up after the timer expires.
< ;script language="javascript">
function count() {
document.getElementByIdx_x_x('m').innerHTML="Timer has started!";
setTimeout("alert('Ten seconds to! ')",10000)
}
< input TYPE="button" value=" Timing starts" onclick="count()">
Example 4: Countdown time jump
My JSP 'ds04.jsp' starting page title>
3
Automatically jump after seconds...
Example 6:
Example 7:
Example 8:
2< /span>
js timer (execute once, repeat)
Share a piece of js code, a small example of js timer, which is divided into a timer that is executed once and a timer that is executed repeatedly. For reference for beginners.
1, a timer that only executes once
<script> <br>//Timer asynchronous Run <br>function hello(){ <br> alert("hello"); <br>} <br>//Use the method name to execute the method <br>var t1 = window.setTimeout(hello,1000); <br>var t2 = window.setTimeout("hello()",3000);//Use string execution method <br>window.clearTimeout(t1);//Remove the timer <br></script>
2, timer for repeated execution
<script> <br>function hello(){ <br> alert("hello"); <br>} <br>//Repeatedly execute a method <br>var t1 = window. setInterval(hello,1000); <br>var t2 = window.setInterval("hello()",3000); <br>//How to remove the timer <br>window.clearInterval(t1); <br>< ;/script><br>
</div>
<p>Remarks: </p>
<p>If there are two methods in a page, both of which are executed after the page is loaded, but actually cannot be executed in order, you can refer to the following method to solve the problem: <br>You can add a timer in the onload method , set a timer, "delay" for a period of time before running, which can be considered to distinguish the order of page loading and running methods. </p>
<p><br><br></p>
<div class="codetitle">
<span><a style="CURSOR: pointer" data="39283" class="copybut" id="copybut39283" onclick="doCopy('code39283')"><u>Copy code</u></a></span> The code is as follows:</div>
<div class="codebody" id="code39283">
<br><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br><html xmlns="http://www.w3.org/1999/xhtml"><br><head runat="server"><br> <title>Untitled Page</title>
<p> <script language="javascript" type="text/javascript"><br> var YC = new Object();<br> function beginYC()<br> {<br> var secondsYC = document.getElementById("txtYCSeconds").value;<br> try<br> { <br> YC = setTimeout("alert('延迟"+secondsYC+"秒成功')",secondsYC*1000);<br> }<br> catch(e)<br> {<br> alert("请输入正确的秒数。");<br> }<br> }<br> function overYC()<br> {<br> clearTimeout(YC);<br> YC=null;<br> alert("终止延迟成功。");<br> }<br><br>/**************************↓↓↓↓定时器的使用↓↓↓↓********************************/<br><br> var timerDS = new Object();<br> var timerDDS = new Object();<br> function beginDS()<br> {<br> sn.innerHTML = "0";<br> timerDS = setInterval("addOne()",parseInt(document.getElementById("txtIntervalSeconds").value,10)*1000);<br> }<br> function goonDS()<br> {<br> timerDS = setInterval("addOne()",parseInt(document.getElementById("txtIntervalSeconds").value,10)*1000);<br> }<br> function overDS()<br> {<br> clearInterval(timerDS);<br> timerDS=null;<br> }<br> function delayDS()<br> {<br> overDS();<br> timerDDS = setTimeout("goonDS()",document.getElementById("txtDDSSeconds").value*1000);<br> }<br> function addOne()<br> {<br> if(sn.innerHTML=="10")<br> {<br> overDS();<br> alert("恭喜你,已成功达到10秒");<br> return;<br> }<br> sn.innerHTML=parseInt(sn.innerHTML,10)+1;<br> }<br><br> </script>