JavaScript settimeout () Countdown Implementation code

Source: Internet
Author: User


<div> from page close and <span id= "Limit" >5</span> sec </div>

  code is as follows copy code
<script type=" Text/javascript ">
<!--
Function Showlimit () {
    for (Var i=5;i>=0;i--) {
        settimeout ("Setlimit" +i+ ")", i*1000);
   }
}
 
function Setlimit (i) {
    var n =-(i-5);
    var obj = document.getElementById ("Limit");
    if (obj) {
        obj.innerhtml = n;
   }
}
Window.onload = function () {
    showlimit ();
}
//-->
</script>

Apply to window closure prompts, page closure prompts, and so on, note that JavaScript negative negative positive this algorithm.

See example one delay loading code

The code is as follows Copy Code

<script type= "Text/javascript" >
function Timedtext ()
{
var t1=settimeout ("document.getElementById" (' txt '). value= ' 2 seconds! ' ", 2000)
var t2=settimeout ("document.getElementById" (' txt '). value= ' 4 seconds! ' ", 4000)
var t3=settimeout ("document.getElementById" (' txt '). value= ' 6 seconds! ' ", 6000)
}
</script>

This is a section of JS code is 2,4,6 seconds to perform once.

Inside the form is as follows.

The code is as follows Copy Code

<input type= "button" value= "Displays the timed text!" "Onclick=" Timedtext () >
<input type= "text" id= "TXT" >

So we can just type in a character and start to see the 2,4,6 show Oh.

For more details please see: http://www.111cn.net/wy/99/e80915fb120d9b233c8471240a7390aa.htm


settimeout implementation of automatic change shows the effect of random numbers:

The code is as follows Copy Code
<script>
Window.onload=sett;
function Sett ()
{
Document.body.innerhtml=math.random ();
settimeout ("Sett ()", 500);
}
</script>
<body>
</body>

Related functions:

The settimeout () method is used to call a function or evaluate an expression after a specified number of milliseconds.

SetTimeout () executes only one code at a time. If you want to call more than once, use SetInterval () or let code itself call SetTimeout () again.

Automatic changes with setinterval to display the effect of random numbers:

  code is as follows copy code

<script>
Function sett ()
{
Document.body.innerhtml=math.random ();
}
SetInterval ("Sett ();", 500);
</script>
</script>
<body>
</body>
/td>

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.