Vue实现验证码倒计时
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>验证码倒计时</title>
<script src="/vue.js"></script>
</head>
<body>
<div id="app">
<button class="button" @click="countDown">
{{content}}
</button>
</div>
<script>
new Vue({
el:'#app',
data () {
return {
content: '发送验证码',
totalTime: 60
}
},
methods: {
countDown () {
this.content = this.totalTime + 's后重新发送'
let clock = window.setInterval(() => {
this.totalTime--
this.content = this.totalTime + 's后重新发送'
if (this.totalTime < 0) {
window.clearInterval(clock)
this.content = '重新发送验证码'
this.totalTime = 60
}
},1000)
},
}
})
</script>
</body>
</html>