To know how they stop, first we need to understand their operating mechanisms and principles:
FirstSetinterval:
Bytes --------------------------------------------------------------------------------------------------------
1. html Dom setinterval () method
Definition and usage
The setinterval () method can call functions or computation expressions according to the specified period (in milliseconds.
The setinterval () method does not stop calling the function until clearinterval () is called or the window is closed. The id value returned by setinterval () can be used as a parameter of the clearinterval () method.
Syntax
setInterval(code,millisec[,"lang"])
| Parameters |
Description |
| Code |
Required. The function to be called or the code string to be executed. |
| Millisec |
Required. The interval between periodical execution or call codes, in milliseconds. |
Return Value
A value that can be passed to window. clearinterval () to cancel periodic code execution.
---------------------------------------------------
2,HTML Dom clearinterval ()Method
Definition and usageThe clearinterval () method can cancel the timeout set by setinterval.
The parameter of the clearinterval () method must be the id value returned by setinterval.
SyntaxclearInterval(id_of_setinterval)
| Parameters |
Description |
| Id_of_setinterval |
Returned by setinterval ()ID value. |
How to stop:
The following example calls the clock () function every 50 milliseconds. You can also use a button to stop the clock:
<HTML>
<Body>
<Input type = "text" id = "Clock" size = "35"/>
<Script language = JavaScript>
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>
</Html>
Bytes --------------------------------------------------------------------------------------------------------
To learn more about setTimeout:
1. html Dom setTimeout () method
Definition and usageThe setTimeout () method is used to call a function or computing expression after a specified number of milliseconds.
SyntaxsetTimeout(code,millisec)
| Parameters |
Description |
| Code |
Required. The javascript code string to be executed after the function to be called. |
| Millisec |
Required. The number of milliseconds to wait before executing the code. |
Tips and commentsNote: setTimeout () is executed only once. If you want to call it multiple times, use setinterval () or let the Code itself call setTimeout () again ().
Instance. In this example, a prompt box will pop up five seconds after you click the button:
-----------------------------
2. html Dom cleartimeout () method
Definition and usageThe cleartimeout () method can cancel the timeout set by the setTimeout () method.
SyntaxclearTimeout(id_of_settimeout)
| Parameters |
Description |
| Id_of_setinterval |
The id value returned by setTimeout. This value identifies the delayed execution code block to be canceled. |
InstanceThe following example calls the timedcount () function once per second. You can also use a button to terminate the scheduled message:
clearTimeout(t) }</script>