JavaScript learning notes timer and javascript learning notes

Source: Internet
Author: User

JavaScript learning notes timer and javascript learning notes

Timer 1

It is used to execute a program after a specific period of time.

SetTimeout ():

Format: [Timer object name =] setTimeout ("<expression>", millisecond)

Function: Run <expression> once.

Example:

Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Title> timer1.html </title>
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "this is 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 successful! ') ", 7000 );
}
</Script>
</Head>
<Body>
<Input type = "button" value = "Click me" onclick = "count ();">
</Body>
</Html>

Timer 2

The expressions are repeatedly executed at intervals.

SetInterval ():

Format: [Timer object name =] setInterval ("<expression>", millisecond)

Function: Repeat <expression> until the window and framework are closed or clearInterval is executed.

ClearInterval ():

Format: clearInterval (timer object name)

Function: terminate a timer.

Example:

Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Title> timer2.html </title>
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "this is 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;
Var timer = setInterval ("count ();", 1000); // The timer starts when the page is loaded.
Function count ()
{
Document. getElementById ("num"). innerHTML = sec ++;
}
Function stopCount ()
{
ClearInterval (timer); // stop the timer.
}
</Script>
</Head>
<Body>
<Font color = "red" id = "num"> 0 </font>
<Input type = "button" value = "stop" onclick = "stopCount ();">
</Body>
</Html>

The above is all the content of this article. I hope you will like it.

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.