type==1是循环添加settimout,type==2是循环终止clearTimeout
第一种,建议
timeOut=[];
shutterAnalysis(type){
if(type == 1) {
//添加的方法
for (let i = 0; i < 10; i++) {
that.timeOut[i] = setTimeout(e=> {
console.log("我添加了");
}, i * 2000);
}
}else {
//清除的方法
for (var j = 0; j < 10; j++) {
if (this.timeOut[j] != undefined) {
clearTimeout(this.timeOut[j]);
}
}
}
}
第二种
timeOut=[];
shutterAnalysis(type){
if(type == 1) {
//添加的方法
for (var i = 0; i < 10; i++) {
(function (i) {
that.timeOut[i] = setTimeout(function () {
console.log("我添加了");
}, i * 2000);
})(i);
}
}else {
//清除的方法
for (var j = 0; j < 10; j++) {
if (this.timeOut[j] != undefined) {
clearTimeout(this.timeOut[j]);
}
}
}
}