JavaScript Learning Notes Timer _ Basics

Source: Internet
Author: User
Tags setinterval time interval

Timer 1

Used to specify a program to be executed after a certain period of time.

SetTimeout ():

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

Function: Execute < expression > once.

Example:

Copy Code code as follows:

<! DOCTYPE html>
<title>timer1.html</title>
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<!--<link rel= "stylesheet" type= "Text/css" href= "./styles.css" >-->
<script type= "Text/javascript" >
function count ()
{
SetTimeout (' Alert (' execution succeeded! ') ", 7000);
}
</script>
<body>
<input type= "button" value= "click Me" onclick= "count ();" >
</body>

Timer 2

Repeats an expression repeatedly, at a certain time interval.

SetInterval ():

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

Function: Repeat execution < expression until the window, frame is closed, or the clearinterval is executed.

Clearinterval ():

Format: clearinterval (Timer object name)

Function: Termination timer

Example:

Copy Code code as follows:

<! DOCTYPE html>
<title>timer2.html</title>
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<!--<link rel= "stylesheet" type= "Text/css" href= "./styles.css" >-->
<script type= "Text/javascript" >
var sec = 0;
The var timer = setinterval ("count ();", 1000);//When the page loads, the timer starts
function count ()
{
document.getElementById ("num"). InnerHTML = sec++;
}
function Stopcount ()
{
Clearinterval (timer);/Stop timer running
}
</script>
<body>
<font color= "Red" id= "num" >0</font>
<input type= "button" value= "Stop" onclick= "Stopcount ();" >
</body>

The above is the entire content of this article, I hope you can enjoy

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.