class myinterval{
constructor(){
this.stop=false
}
on(func,wait){
var that=this;
var timer=setTimeout(function(){
if(that.stop){
clearTimeout(timer)
}else{
func();
that.on(func,wait);
}
},wait)
}
off(){
console.log('关闭')
this.stop=true;
}
}
var left=document.getElementsByClassName('left')[0]
var two=new myinterval()
two.on(function(){
console.log('使用setTimeout实现setInterval')
},1000)
setTimeout(function(){
two.off();
},3000)
前端基础笔记——通过setTimeout实现setInterval
最新推荐文章于 2024-08-08 10:10:46 发布