JavaScript timer details

Source: Internet
Author: User

There are two dedicated functions for timers in javascrui:

1. Countdown timer: timename = setTimeout ("function ();", delaytime );

2. Cyclic Timer: timename = setInterval ("function ();", delaytime );

Function () is the event function to be executed when the timer is triggered. It can be a function, several functions, or javascript statements, which can be separated by commas; delaytime is the interval, in milliseconds.

The countdown timer triggers events after a specified time, while the cyclic timer triggers events repeatedly when the time interval arrives. The difference is that the former only works once, while the latter does not stop acting.

The countdown timer is generally used when only one trigger is required on the page. For example, after clicking a button, the page jumps to the corresponding site after a certain time, it can also be used to determine whether a viewer is an "old customer" on your site. If not, you can jump to the corresponding site in 5 or 10 seconds, then, let him know that he can click a button in a certain place to quickly enter.

The cyclic timer is generally used for the effect that needs to be executed from the site. For example, a javascript scroll bar or status bar can also be used to represent the page background with a flying snow image. These events need to run every other time.

Sometimes we also want to remove some additional timers. At this time, we can use clearTimeout (timename) to disable the countdown timer, and use clearInterval (timename) to disable the cyclic timer.


Example 1:
<Script language = "JavaScript">
SetTimeout ("confirm ('20140901')", 123456 );
</SCRIPT>

<Script language = "JavaScript" type = "text/javascript">


Function count (){
SetTimeout ("alert (three seconds to ')", 3000)
}

</Script>

<Input type = "button" VALUE = "Start Time" 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 website. Please join us! ";
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 () ', 500)">
<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.