Several nice js countdown program instances

Source: Internet
Author: User
Tags time in milliseconds

To implement countdown in js, we need to use the setTimeout function. setTimeout can be used to set the execution time or place the display time in the input, div, or other containers.

1. Simple timing pop-up window:

In this instance, we will create a button on the page. When this button is clicked, the page will jump out of the warning window 3 seconds later.

You can test the Code as follows:

The Code is as follows: Copy code

// Simple javascript timing
<Html>

<Head>
<Script type = "text/javascript">
// Create a function to execute the timer window
Function timedMsgDemo ()
{
// Declare the variable to receive 3-second pop-up windows. The unit of time is milliseconds.
Var timeDemo = setTimeout ("alert ('3 seconds pop-up! ') ", 3000)
}
</Script>
</Head>
<Body>
<Form>
// Create a button to trigger the event
<Input type = "button" value = "click the button. A pop-up window is displayed three seconds later." onClick = "timedMsgDemo ()">
</Form>
<P> when you click this button, the page will pop up in 3 seconds! </P>
</Body>
</Html>

2. Timing effects for displaying the reservation time:

In this instance, we set a few times in advance. When the page shows this time, the page will show the times we have reserved

Time, through this instance, we can flexibly implement a lot of timing effects, such as how long each page exceeds

The page will automatically display the user's logon time prompt. Please refer to the following sample code and you can expand it yourself:

The Code is as follows: Copy code

// Pre-set several times. Each time the page is opened, different texts are displayed on the page.
<Html>
<Head>
<Script type = "text/javascript">
Function timedTextDemo ()
{
// Be sure to pay attention to the unit of time in milliseconds
Var txt1 = setTimeout ("document. getElementById ('txt '). value =' You have been online for 1 minute! Welcome to the primary account

Come! '", 60000)
Var txt2 = setTimeout ("document. getElementById ('txt '). value =' You have been online for 10 minutes! Little joy!

'", 600000)
Var txt3 = setTimeout ("document. getElementById ('txt '). value =' You have been online for 1 hour! Big brother hurt,

Please take care of your health! '", 3600000)
}
</Script>
</Head>
<Body>
<Form>
<Input type = "button" value = "click the button to display different texts at intervals" onClick = "timedTextDemo

() ">
// Create a text box to display the text to be output
<Input type = "text" id = "txt">
</Form>
<P> in this example, when the page is opened for 1 minute, 10 minutes, or 1 hour,

Same text </p>
</Body>
</Html>

3. Simple clock effects:

This special effect is quite simple, but it is also the most practical special effect. You can analyze it with the knowledge of the last two articles. It is very simple,

If you expand it on your own, it will work very well. Please refer to the following code:

The Code is as follows: Copy code

<Html>
// Simple small clock effects are displayed on the page
<Head>
<Script type = "text/javascript">
// Create a function to display the clock
Function startTimeDemo ()
{
// Create a new time object
Var today = new Date ()
// Declare a variable to receive hours
Var h = today. getHours ()
// Receipt in minutes
Var m = today. getMinutes ()
// Receipt seconds
Var s = today. getSeconds ()
M = checkTimeDemo (m)
S = checkTimeDemo (s)
Document. getElementById ('txt '). innerHTML = h + ":" + m + ":" + s
T = setTimeout ('starttimedemo () ', 500)
}
// Add a value of 0 in front of the number. When the value is smaller than 10
Function checkTimeDemo (I)
{
If (I <10)
{I = "0" + I}
Return I
}
</Script>
</Head>
<Body onload = "startTimeDemo ()">
<Div id = "txt"> </div>
</Body>
</Html>

Now, we will provide three instances for your reference. Please stay tuned to the subsequent content!

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.