A brief analysis of JavaScript timer _ basic knowledge

Source: Internet
Author: User
Tags setinterval

SetTimeout ()--Used to specify a program to be executed after a certain period of time.

Format:

[Timer object name =]settimeout ("< expression >", number of milliseconds);

Function: Execute < expression > once.

Where an expression is a string, you can make any JavaScript statement

Copy Code code as follows:

<script type= "Text/javascript" >
Execute alert after 5 seconds.
function count () {
SetTimeout ("Alert (' execution success ');", 5000);
}
</script>
<body>
<input type= "button" value= "Execute" onclick= "count ()" >
</body>

SetInterval ()-Repeat execution < expression until the window, frame is closed, or executed clearinterval

Format: [Timer object name =]setinterval ("< expression >", ms)

Clearinterval () Termination timer

Format: clearinterval (Timer object name)

Copy Code code as follows:

<script type= "Text/javascript" >
var sec=0;
var timeid=setinterval ("Count ();", 1000);

function count () {
document.getElementById ("num"). innerhtml=sec++;
}

function Stopcount () {
Clearinterval (Timeid);
}
</script>
<body>
<font style= "color:red" id= "num" >0</font> sec <input type= "button" value= "Stop" onclick= "Stopcount ();" >
</body>

is not very easy to use, there is a need for small partners under the reference bar.

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.