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= "http://www.softwhy.com/" /><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= "http://www.softwhy.com/" /><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