vue-element 引用 echarts的for循环中使用setTimeout和清除clearTimeout
1、首先在data里定义timer1: []
data() {
return {
timer: []
}
}
2、在methods方法里使用setTimeout
const $this = this
for (let i = 0; i < 10; ++i) {
(function(i) {
$this.timer1[i] = setTimeout(function() {
$this.updateYear($this.years[i + 1])
}, (i - 10) * 2000)
})(i)
}
3、在beforeDestory里使用 clearTimeout
beforeDestroy() {
const _this = this
for (let i = 0; i < 10; ++i) {
clearTimeout(_this.timer1[i])
}
}