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.