HTML DOM setinterval () and Clearinterval () methods
The setinterval () method can call a function or a calculation expression in the specified period (in milliseconds). The SetInterval () method keeps calling the function 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.
The clearinterval () method cancels the timeout set by SetInterval (). The parameter of the Clearinterval () method must be the ID value returned by SetInterval ().
Case one: Implement a timer, require
1, from start to end (including start and last), every 100 milliseconds console.log a number, each number increase to 1
2, the returned object needs to include a Cancel method to stop the timed operation
3. The first number needs immediate output
function count (start, end) {
if (start <=end) {
console.log (start++);
Xin=settimeout (function () {
count (start,end);
},100);
return {
cancel:function () {
cleartimeout (xin);
}
}
}
HTML DOM setinterval () and Clearinterval () methods
The settimeout () method is used to call a function or evaluate an expression after a specified number of milliseconds.
The cleartimeout () method cancels the timeout set by the settimeout () method.
Case two: A timer with a stop button