Home >Web Front-end >JS Tutorial >JavaScript instance parsing clear timer
This article brings you relevant knowledge about javascript, which mainly introduces issues related to timers and clearing timers. You can use the clearTimeout method and clearInterval method to clear the specified timer. Device, let’s take a look at it together, I hope it will be helpful to everyone.
[Related recommendations: javascript video tutorial, web front-end】
window.setTimeout(call function, delay time);
This window can omit this delay when calling
setTimeout(function(){ console.log('你好'); },2000);//2秒后才在控制台输出 你好
setInterval(function(){ console.log('你好'); },2000)//每隔2秒在控制台输出一次你好,不清除定时器会一直运行
var timer = setTimeout(function() { console.log('你好!'); },5000); clearTimeout(timer);//上面一个定时器就不会在执行程序clearInterval clear timerFollow The same as the above timer, it is used to clear the setInterval timer. You also need to give the timer a name. Syntax: clearInterval (timer name)
var times = setInterval(function(){ console.log('你好!'); },1000); setTimeout(function(){ clearInterval(times); //5秒后清除定时器 },5000);Examples are as follows: We sometimes write multiple timers and create the timers without saving them in variables. At this time, we cannot clear them directly. Then we can write a method to clear all timers in the pageTo clear the timer, you must first understand what the return value is
javascript video tutorial, web front end】
The above is the detailed content of JavaScript instance parsing clear timer. For more information, please follow other related articles on the PHP Chinese website!