JavaScript Timer detailed

Source: Internet
Author: User
Tags setinterval time interval

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.