clearInterval和setTimeout区别
settimeout只运行一次,也就是说设定的时间到后就触发运行指定代码,运行完后即结束。setTimeout()只执行函数一次,如果需要多次调用可以使用setInterval(),或者在函数体内再次调用setTimeout()
var timeoutID = window.setTimeout(func, delay, [param1, param2, …]);
timeoutID 是该延时操作的数字ID, 此ID随后可以用来作为window.clearTimeout方法的参数.
func 是你想要在delay毫秒之后执行的函数.
setinterval是循环运行的,即每到设定时间间隔就触发指定代码。这是真正的定时器。
var timer = window.setInterval(func, delay[, param1, param2, …]);
timer 是此重复操作的唯一辨识符,可以作为参数传给clearInterval()。
func 是你想要重复调用的函数。
清除方法:
window.clearInterval(timer )
//调用
this.timeSender = window.setInterval(() => {
setTimeout(this.selectReviewRecordStatus, 10)
}, 5000)
//清除
window.clearInterval(this.timeSender);
this.timeSender = null;
js 中实现自动滑动
<button id="btn">开始</button>
<div class="box" id="bOX">
var btn = document.getElementById("btn");
var box = document.getElementById("bOX");
var num = 0;
var timer = null;
btn.onclick = function() {
timer = setInterval(function(){
num++;
if(num >=500)
{
clearInterval(timer);
}
else
{
box.style.left = num + "px";
}
},10)
}