to get to the point, first talk about the timer.
In JAVASCRITP, there are two dedicated functions for timers, respectively:
1. Inverted timer: Timename=settimeout ("function ();", delaytime);
2. Cycle Timer: Timename=setinterval ("function ();", delaytime);
The first parameter "function ()" Is the action to be performed when the timer is triggered, it can be a function, or it can be several functions, separated by ";" between functions. For example, to eject two warning windows, you can "function ()". Into
"Alert (' First warning window! '); Alert (' Second warning window! '); , and the second parameter "Delaytime" is the time of the interval, in milliseconds, that is, fill in "5000", which means 5 seconds.
The countdown timer is to trigger an event at a specified time, and the loop timer is a recurring event when the time interval arrives, the difference being that the former is only a function once and the latter is constantly acting.
For example, when you open a page, want to interval a few seconds to automatically jump to another page, you need to use the inverted timer "settimeout (" function (); ", Delaytime)", and if you want to set a sentence into a word of the appearance,
You need to use the loop timer "setinterval" ("function ();", Delaytime).
Gets the focus of the form, the document.activeElement.id is used. Use if to determine whether the ID of the document.activeElement.id and form is the same.
For example: if ("mid" = document.activeElement.id) {alert ();}, "Mid" is the corresponding ID of the form.
Two examples are briefly cited.
Example 1. A verbatim output string when a form is triggered or loaded
Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Untitled Document </title>
<script language= "JavaScript" type= "Text/javascript" >
var str = "This is the sample text for testing";
var seq = 0;
var second=1000; Time interval 1 seconds
function Scroll () {
msg = str.substring (0, seq+1);
document.getElementById (' word '). InnerHTML = msg;
seq++;
if (seq >= str.length) seq = 0;
}
</script>
<body onload= "setinterval (' scroll () ', second)" >
<div id= "word" ></div><br/><br/>
</body>
Example 2. When the focus is in the input box, check the input box information periodically, and do not perform the check action when the focus is not.
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Untitled Document </title>
<script language= "JavaScript" type= "Text/javascript" >
var second=5000; Time interval 5 Seconds
var c=0;
function Scroll () {
C + +;
if ("b" = = Document.activeElement.id) {
var str= "Regular check <b>" +c+ "</b> times <br/>";
if (document.getElementById (' B '). value!= "") {
str+= the current content of the input box is <br/><b> +document.getelementbyid (' B '). value+ "</b>";
}
document.getElementById (' word '). InnerHTML = str;
}
}
</script>
<body>
<textarea id= "B" name= "B" style= "height:100px; width:300px "onfocus=" setinterval (' scroll () ', second) ' ></textarea><br/><br/>
<div id= "word" ></div><br/><br/>
</body>
Example 3, the following is the simplest example, the timer time arrived after the pop-up warning window.
Copy Code code as follows:
& Lt;meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<script language=" javascript ">
function Count () {
document.getElementById (' m '). Innerhtml= "The timer has started! ";
settimeout (' 10 seconds to! ') ', 10000
}
</script>
<body>
<div id= "M" ></div>
<input type= "but Ton "value=" timer start onclick= "count ()" >
</body>