javascript中setInterval越来越快的问题解决方法


var timer
function run(){
	//clearInterval要放在方法开始,不然的话,下面的代码还没运行到clearInterval,又开始了循环了。
        if(timer){
            clearInterval(timer)
       } 
	//do something....
	timer = setInterval(()=>{
		run()
	},1000)
}

run()  

或者

function run(){
	//do something....
	setTimeout(()=>{
		run()
	},1000)
}

run()

  

posted @ 2022-12-13 14:14  明明一颗大白菜  阅读(151)  评论(0编辑  收藏  举报
<-- -->