// setInterval定时器 倒计时十秒
test3() {
let that = this
let {
num
} = that.data
let t = setInterval(() => {
console.log(“倒计时”, num)
if (0 <= num) {
that.setData({
num: num–
})
} else {
clearTimeout(t)
}
}, 1000);
},
//setTimeout定时器 倒计时十秒
test4(s) {
let that = this
setTimeout(() => {
if (s > 0) {
s–
console.log(“倒计时”, s)
that.setData({
num: s
})
that.test4(s)
} else {
console.log(“跳出”)
}
}, 1000);
},
以上两种都可以完成小程序倒计时10秒
onLoad中:this.test4(10)或this.test3()
html中:{{num}}