index.vue
730 字节
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<template>
<view class="Countdown">
<view v-if="remainingTime === 0" @click="startCountdown">获取验证码</view>
<view v-else>{{ remainingTime }}s 后重新获取</view>
</view>
</template>
<script>
export default {
name: 'Countdown',
data() {
return {
remainingTime: 0,
timer: null
}
},
methods: {
startCountdown() {
this.$emit('start')
},
start() {
this.remainingTime = 60;
this.timer = setInterval(() => {
this.remainingTime--;
if (this.remainingTime === 0) {
clearInterval(this.timer);
}
}, 1000);
}
},
beforeDestroy() {
clearInterval(this.timer);
}
}
</script>
<style lang="scss" scoped>
.Countdown{
font-size: 26rpx;
}
</style>