JavaScript setinterval settimeout Instance (1/2)

Source: Internet
Author: User

The

1,html Dom SetInterval () method
Definition and Usage
SetInterval () method can call a function or a calculation expression in the specified period (in milliseconds). The
SetInterval () method calls the function continuously until the clearinterval () is called or the window is closed. The ID value returned by setinterval () can be used as an argument to the Clearinterval () method.
Syntax
setinterval (code,millisec[, "Lang"])
Parameter description
code required. The function to invoke or the code string to execute.
Millisec must be. The time interval, in milliseconds, between the periodic execution or invocation of code.
return value
A value that can be passed to Window.clearinterval () to cancel the periodic execution of code. The
2,html Dom Clearinterval () method
Definition and Usage
Clearinterval () method cancels the timeout set by SetInterval (). The parameter of the
Clearinterval () method must be the ID value returned by setinterval (). The
Syntax
Clearinterval (id_of_setinterval)
Parameter Description
Id_of_setinterval the ID value returned by setinterval ().
How to stop:
The following example calls the clock () function every 50 milliseconds. You can also use a button to stop this clock: 
 

<body>
<input type= "text" id= "clock" size= ""/>
<script language= Web page Special effects >
var int=self.setinterval ("Clock ()", 50)
function Clock ()
{
var t=new date ()
document.getElementById ("Clock"). value=t
}
</script>
</form>
<button onclick= "int=window.clearinterval (int)" >
Stop interval</button>
</body>

[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
To learn more about settimeout:
1,html Dom SetTimeout () method
Definitions and usage
The settimeout () method is used to call a function or evaluate an expression after a specified number of milliseconds.
Grammar
SetTimeout (CODE,MILLISEC)
Parameter description
Code required. The JavaScript code string to execute after the function to be invoked.
Millisec required. The number of milliseconds to wait before executing the code.
Tips and comments
Tip: 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.
Example, this example, after you click the button for 5 seconds will pop up a prompt box:

<script type= "Text/javascript" >
function timedmsg ()
{
var t=settimeout ("Alert (' 5 seconds! ')", 5000)
}
</script>
<body>
<form>
<input type= "button" value= "Display timed alertbox!"
Onclick= "timedmsg ()" >
</form>
<p>click on the button above. An alert box would be
Displayed after 5 seconds.</p>
</body>

Home 1 2 last page

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.