SetInterval () function usage detailed

Source: Internet
Author: User

SetInterval () function usage in detail:
This function is widely used, in the scrolling code or focus pictures and other effects are applied, the following is a simple example of the use of this function.
The SetInterval () function can stipulate that a function is executed at a specified period of time, that is, every certain event begins to execute the specified function once.
The syntax is as follows:

SetInterval (Code,interval)

This function has two parameters, the first parameter specifies the function to execute, the second parameter specifies the interval between functions two executions, in milliseconds (1 seconds =1000 milliseconds).
The code example is as follows:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"/><Metaname= "Author"content= "" /><title>Ant Tribe</title><Scripttype= "Text/javascript">window.onload=function(){  varmytest=document.getElementById ("mytest"); varFlag=0; varx=setinterval (MyTime, +); functionMyTime () {flag=Flag+1; Mytest.innerhtml=Flag; }}</Script></Head><Body>  <DivID= "MyTest"></Div></Body></HTML>

The above code can execute the MyTime () function once every second through the setinterval () function, each time this function is executed, flag is added 1, and the flag is written to the Div.
This function is typically used in conjunction with the Clearinterval () function, and the setinterval () function can return a unique value for the current setinterval () function, so clearinterval () The function can stop the execution of the corresponding Setinterva () function with this marked value as a parameter, as the above code is modified as follows:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"/><Metaname= "Author"content= "" /><title>Ant Tribe</title><Scripttype= "Text/javascript">window.onload=function(){  varmytest=document.getElementById ("mytest"); varBT=document.getElementById ("BT"); varFlag=0; varx=setinterval (MyTime, +); functionMyTime () {flag=Flag+1; Mytest.innerhtml=Flag; } Bt.onclick=function() {clearinterval (x); }}</Script></Head><Body>  <DivID= "MyTest"></Div>  <ButtonID= "BT">Stop execution</Button></Body></HTML>

Click the button to end the execution of the setinterval () function.

The original address is: setinterval () function usage in a detailed chapter.

SetInterval () function usage detailed

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: 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.