<template>
<div class="component">
<el-form :model="noticeForm" :rules="rules" ref="noticeForm">
//.....
<div class="btn_wrap">
<el-button @click="handleAgree()" :disabled="checkboxDisabled" type="primary" size="small">
我已阅读并同意
<span v-if="num">{{num}}s</span>
</el-button>
</div>
</el-form>
</div>
</template>
<script>
export default {
data () {
return {
timer: null,
num: 5,
}
},
computed: {
checkboxDisabled () {
return this.num !== 0
}
},
mounted () {
this.setNum()
},
methods: {
handleDisagree () {
this.$emit('close')
},
setNum () {
this.timer = setInterval(() => {
console.log('我是定时器,我跑的很快')
if (this.num > 0) {
this.num--
} else {
this.clearIntervalTimer()
}
}, 1000)
this.$once('hook:beforeDestroy', () => {
this.clearIntervalTimer()
})
},
clearIntervalTimer () {
window.clearInterval(this.timer)
}
}
}
</script>
<style lang="scss" scoped>
.component {
color: $color-text;
.btn_wrap {
display: flex;
justify-content: center;
padding-bottom: 20px;
/deep/ .el-button {
margin-left: 16px !important;
}
}
}
</style>