定时器分为两种,setTimeout
(不重复)与setInterval
(重复)
都接受两个参数(参数一:所使用的函数,参数二:时间)
export default {
name: "定时器"
data() {
return {
}
}
},
mounted() {
this.$nextTick(() => {
this.timer = setInterval(this.abc, 1000) // 使用定时器
this.timer = setTimeout(this.abc, 1000)
})
},
beforeDestroy() {
clearInterval(this.timer) //清除定时器
clearTimeout(this.timer)
},
methods: {
abc() {
console.log("我使用了定时器") // 定时器使用的函数
}
}
}