In JAVASCRITP, there are two specialized functions for timers, which are:
1. Inverted timer: Timename=settimeout ("function ();", delaytime);
2. Cycle Timer: Timename=setinterval ("function ();", delaytime);
function () is the event of the timer trigger to perform the functions, can be a function, or can be a few functions, or JavaScript can also be a statement, the single to use; Delaytime is the time interval, in milliseconds.
Countdown timer is to trigger the event after a specified time, and the cycle timer is the interval between the arrival of the recurrence of events, the difference is: the former only function once, while the latter is constantly acting.
The countdown timer is typically used on a page that only needs to be triggered once, for example, click on a button after the page in a certain time to jump to the corresponding site, can also be used to determine whether a browser is your site "old guest", if not, you can in 5 seconds or 10 seconds to jump to the corresponding site, And tell him to come back later. You can click a button in a place to quickly enter.
Cycle timers are typically used on sites that need to be performed from a duplicate, such as a JavaScript scroll bar or a status bar, and can be used to represent the background of a page with a blizzard picture. These events take time to run at intervals.
Sometimes we also want to remove some of the added timer, at this time can use Cleartimeout (timename) to close the countdown timer, and with Clearinterval (Timename) to turn off the cycle timer.
Example 1:
<script language= "JavaScript" >
SetTimeout ("Confirm (' 123456 ')", 1000);
</SCRIPT>
<script language= "JavaScript" type= "Text/javascript" >
function count () {
SetTimeout ("alert (' three Seconds to ')", 3000)
}
</Script>
<input type= "button" value= "timer start" onclick= "Count ()" >
Example 2:
<script><script language= "JavaScript" type= "Text/javascript" >
var sec = 0;
Timerid = SetInterval ("Count ()", 1000);
function count () {
num.innerhtml = sec++;
}
</Script>
Stay time:
<font id= "num" face= "impact" >0</FONT> seconds
<input type= "button" value= "Stop" onclick= "Clearinterval (Timerid)" >
Example 3:
<script language= "JavaScript" type= "Text/javascript" >
var str = "This is an online auction site, please do your shopping!" ";
var seq = 0;
function Scroll () {
msg = str.substring (0, seq+1);
banner.innerhtml = msg;
seq++;
if (seq >= str.length) seq = 0;
}
</Script>
<body onload= "setinterval (' scroll () ')" >
<font id= "banner" ></FONT>
</Body>