vue怎么实现发送验证码计时器防止刷新

其他教程   发布日期:2023年08月10日   浏览次数:419

这篇文章主要介绍“vue怎么实现发送验证码计时器防止刷新”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“vue怎么实现发送验证码计时器防止刷新”文章能帮助大家解决问题。

基本实现效果

按钮:

  1. <t-button @click="handleSend" :disabled="disable">{{text}}</t-button>

data:

  1. text: '发送验证码',
  2. time: 10,
  3. timer: null,
  4. disable: false

点击发送:

  1. handleSend() {
  2. this.disable = true
  3. this.text = this.time + 's后重新发送'
  4. this.timer = setInterval(() => {
  5. if (this.time > 0) {
  6. this.time--
  7. this.text = this.time + 's后重新发送'
  8. } else {
  9. clearInterval(this.timer)
  10. this.time = 10
  11. this.disable = false
  12. this.text = '重新发送'
  13. }
  14. }, 1000)
  15. }

防止刷新

  1. handleSend() {
  2. this.disable = true
  3. this.text = this.time + 's后重新发送'
  4. this.timer = setInterval(() => {
  5. if (this.time > 0) {
  6. this.time--
  7. this.text = this.time + 's后重新发送'
  8. localStorage.setItem('time', this.time) // 注意这行
  9. } else {
  10. clearInterval(this.timer)
  11. this.time = 10
  12. this.disable = false
  13. this.text = '重新发送'
  14. }
  15. }, 1000)
  16. }
  1. created() {
  2. const time = localStorage.getItem('time')
  3. if (time && time > 0) {
  4. this.text = time + 's后重新发送'
  5. this.time = time
  6. this.handleSend()
  7. }
  8. }

以上就是vue怎么实现发送验证码计时器防止刷新的详细内容,更多关于vue怎么实现发送验证码计时器防止刷新的资料请关注九品源码其它相关文章!