将这段代码写成vue3格式,startCountdown() { let count = 60; this.countdown = `${count}秒后重新获取`; this.isDisabled = true; this.timer = setInterval(() => { count--; if (count <= 0) { clearInterval(this.timer); this.timer = null; this.countdown = '获取验证码'; this.isDisabled = false; } else { this.countdown = `${count}秒后重新获取`; } }, 1000); }
时间: 2023-06-11 08:07:50 浏览: 53
<script>
import { ref } from 'vue';
export default {
setup() {
const countdown = ref('获取验证码');
const isDisabled = ref(false);
let timer = null;
const startCountdown = () => {
let count = 60;
countdown.value = `${count}秒后重新获取`;
isDisabled.value = true;
timer = setInterval(() => {
count--;
if (count <= 0) {
clearInterval(timer);
timer = null;
countdown.value = '获取验证码';
isDisabled.value = false;
} else {
countdown.value = `${count}秒后重新获取`;
}
}, 1000);
}
return {
countdown,
isDisabled,
startCountdown
}
}
}
</script>