setTimeout()与setInterval()的区别

本人觉得最主要的区别在于:setTimeout()运用在延迟一段时间,再进行某项操作,setInterval()运用在每个一段时间执行一次某种操作。

1.setTimeout()

1)setTimeout("function",time) 设置一个超时对象 

2)clearTimeout(对象) 清除已设置的setTimeout对象

在项目中运用到的例子,这个是2秒后进行跳转:

                             window.setTimeout(()=>{
                            this.sucessPhoneCheck = false;
                            this.safePhoneCheck = false;
                            this.phoneAccount = false;
                            document.body.style.overflow = '';
                        }, 2000);


2.setInterval()

1) setInterval("function",time) 设置一个超时对象

2) clearInterval(对象) 清除已设置的setInterval对象

在项目中运用到的方法,这个是一个时间倒计时

		let interval = window.setInterval(function() {
                    if ((me.time1--) <= 0) {
                        me.testCode = false;
                        me.time1 = 60;
                        window.clearInterval(interval);
                    }
                }, 1000);


猜你喜欢

转载自blog.csdn.net/smile_monica/article/details/78202971