1. The setInterval () :
Calls the function at the specified period in milliseconds.
The method keeps calling the function until clearInterval() is called or the window is closed.
Grammar:
setInterval(code,millisec,[arg1, arg2, ...] )Copy the code
parameter | describe |
---|---|
code | A necessity. The string of code to execute. |
millisec | Must be. Time interval, in milliseconds. |
arg1.arg2. | Optional. When the timer expires, additional arguments are passed to the func function |
2. The setTimeout () :
Calls the function after the specified number of milliseconds.
grammar
setTimeout(code,millisec,[arg1, arg2, ...] ); // do the same as aboveCopy the code
The final code demo:
//1. You can write the arrow function directly
setTimeout(() = >{
this.isSkeleton=false;
},2000)
//2. Return a value and call clearTimeout() to cancel the timer;
let a = setTimeout(() = >{
alert('pop up');
},5000);
function b() {
window.clearTimeout(a); // After 5 seconds, the popup can be cancelled directly by calling b function
}
Copy the code
The return value:
The return value intervalID is a non-zero value that identifies the timer created by setInterval() and can be used as an argument to clearInterval() to clear the corresponding timer. Note that setInterval() and setTimeout() share the same ID pool, so avoid mixing them;
Finally, the knowledge of popular science:
JS objects can be obtained in two ways, one is developer custom; The second is provided by ECMAScript. Objects provided by ECMAScript are called JavaScript built-in objects.
The timer belongs to the window object provided, timer can also be added in front of the window call
- Male _ no. ❤; Front-end honest person, you can exchange and learn with small partners!