Vue倒計時詳解

Vue倒計時是一個常見的需求,可用於各類業務場景中,如秒殺活動,訂單支付限時等。在Vue中,我們可以通過一些簡單的方法實現倒計時功能,本文將從多個方面詳細介紹Vue倒計時的相關知識。

一、Vue倒計時怎麼做

Vue倒計時可以使用Vue.js提供的計時器方法,通過setInterval()和clearInterval()實現倒計時效果。我們需要將計時器方法封裝成一個組件,在組件中可以通過props接收父組件傳入的倒計時時間,實現倒計時效果。以下是一個簡單的Vue倒計時組件實現:


<template>
  <div>
    {{ time }}
  </div>
</template>

<script>
export default {
  props: {
    totalTime: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      time: ''
    }
  },
  created () {
    this.countDown()
  },
  methods: {
    countDown () {
      let time = this.totalTime
      const tid = setInterval(() => {
        if (time > 0) {
          time --
          this.time = this.formatTime(time)
        } else {
          clearInterval(tid)
        }
      }, 1000)
    },
    formatTime (time) {
      const minute = Math.floor(time / 60)
      const second = time % 60
      return `${minute < 10 ? '0' + minute : minute}:${second < 10 ? '0' + second : second}`
    }
  }
}
</script>

二、Vue倒計時結束自動加載數據

在某些業務場景中,倒計時結束後需要自動加載數據,那麼我們可以在Vue倒計時組件中加入回調函數,在計時結束的時候執行回調函數即可。以下是一個示例:


<template>
  <div>
    {{ time }}
  </div>
</template>

<script>
export default {
  props: {
    totalTime: {
      type: Number,
      required: true
    },
    callback: {
      type: Function,
      required: false
    }
  },
  data () {
    return {
      time: ''
    }
  },
  created () {
    this.countDown()
  },
  methods: {
    countDown () {
      let time = this.totalTime
      const tid = setInterval(() => {
        if (time > 0) {
          time --
          this.time = this.formatTime(time)
        } else {
          clearInterval(tid)
          if (this.callback) {
            this.callback()
          }
        }
      }, 1000)
    },
    formatTime (time) {
      const minute = Math.floor(time / 60)
      const second = time % 60
      return `${minute < 10 ? '0' + minute : minute}:${second < 10 ? '0' + second : second}`
    }
  }
}
</script>

三、Vue倒計時組件

為了方便多次使用,我們可以將Vue倒計時組件封裝成一個單獨的組件,供其他組件進行引用。在該組件中,我們可以提供一些預設的屬性和方法,如總時間,回調函數等。以下是一個Vue倒計時組件的示例:


<template>
  <div class="countdown">
    {{ time }}
  </div>
</template>

<script>
export default {
  name: 'Countdown',
  props: {
    totalTime: {
      type: Number,
      required: true,
      default: 60
    },
    callback: {
      type: Function,
      required: false
    }
  },
  data () {
    return {
      time: ''
    }
  },
  created () {
    this.countDown()
  },
  methods: {
    countDown () {
      let time = this.totalTime
      const tid = setInterval(() => {
        if (time > 0) {
          time --
          this.time = this.formatTime(time)
        } else {
          clearInterval(tid)
          if (this.callback) {
            this.callback()
          }
        }
      }, 1000)
    },
    formatTime (time) {
      const minute = Math.floor(time / 60)
      const second = time % 60
      return `${minute < 10 ? '0' + minute : minute}:${second < 10 ? '0' + second : second}`
    }
  }
}
</script>

四、Vue倒計時5秒

如果我們需要實現一個5秒倒計時,代碼實現非常簡單,只需要將Vue倒計時組件中的totalTime屬性設置為5即可。以下是一個示例:


<template>
  <div class="countdown">
    {{ time }}
  </div>
</template>

<script>
export default {
  name: 'Countdown',
  props: {
    totalTime: {
      type: Number,
      required: true,
      default: 5
    },
    callback: {
      type: Function,
      required: false
    }
  },
  data () {
    return {
      time: ''
    }
  },
  created () {
    this.countDown()
  },
  methods: {
    countDown () {
      let time = this.totalTime
      const tid = setInterval(() => {
        if (time > 0) {
          time --
          this.time = this.formatTime(time)
        } else {
          clearInterval(tid)
          if (this.callback) {
            this.callback()
          }
        }
      }, 1000)
    },
    formatTime (time) {
      const minute = Math.floor(time / 60)
      const second = time % 60
      return `${minute < 10 ? '0' + minute : minute}:${second < 10 ? '0' + second : second}`
    }
  }
}
</script>

五、Vue倒計時60秒

同樣的,如果我們需要實現一個60秒倒計時,只需要將Vue倒計時組件中的totalTime屬性設置為60即可。以下是一個示例:


<template>
  <div class="countdown">
    {{ time }}
  </div>
</template>

<script>
export default {
  name: 'Countdown',
  props: {
    totalTime: {
      type: Number,
      required: true,
      default: 60
    },
    callback: {
      type: Function,
      required: false
    }
  },
  data () {
    return {
      time: ''
    }
  },
  created () {
    this.countDown()
  },
  methods: {
    countDown () {
      let time = this.totalTime
      const tid = setInterval(() => {
        if (time > 0) {
          time --
          this.time = this.formatTime(time)
        } else {
          clearInterval(tid)
          if (this.callback) {
            this.callback()
          }
        }
      }, 1000)
    },
    formatTime (time) {
      const minute = Math.floor(time / 60)
      const second = time % 60
      return `${minute < 10 ? '0' + minute : minute}:${second < 10 ? '0' + second : second}`
    }
  }
}
</script>

六、Vue倒計時功能

在倒計時過程中,我們可能需要實現一些特殊的功能,如暫停和繼續,重新開始等。以下是一個帶有暫停和繼續功能的Vue倒計時組件:


<template>
  <div class="countdown">
    {{ time }} {{ status }}
    <button @click="pause">暫停</button>
    <button @click="resume">繼續</button>
  </div>
</template>

<script>
export default {
  name: 'Countdown',
  props: {
    totalTime: {
      type: Number,
      required: true,
      default: 60
    },
    callback: {
      type: Function,
      required: false
    }
  },
  data () {
    return {
      time: '',
      status: '進行中',
      tid: null
    }
  },
  created () {
    this.countDown()
  },
  methods: {
    countDown () {
      let time = this.totalTime
      this.tid = setInterval(() => {
        if (time > 0) {
          time --
          this.time = this.formatTime(time)
        } else {
          clearInterval(this.tid)
          if (this.callback) {
            this.callback()
          }
        }
      }, 1000)
    },
    formatTime (time) {
      const minute = Math.floor(time / 60)
      const second = time % 60
      return `${minute < 10 ? '0' + minute : minute}:${second < 10 ? '0' + second : second}`
    },
    pause () {
      clearInterval(this.tid)
      this.status = '已暫停'
    },
    resume () {
      this.countDown()
      this.status = '進行中'
    }
  }
}
</script>

七、Vue倒計時方法

除了使用計時器的方法進行倒計時之外,Vue提供了一些其他的方法,如watch和computed。我們可以通過這些方法輕鬆的實現倒計時效果。以下是一個使用watch方法實現的Vue倒計時組件:


<template>
  <div class="countdown">
    {{ time }}
  </div>
</template>

<script>
export default {
  name: 'Countdown',
  props: {
    leftTime: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      time: ''
    }
  },
  created () {
    this.countDown()
  },
  watch: {
    leftTime (newVal) {
      clearInterval(this.tid)
      this.countDown()
    }
  },
  methods: {
    countDown () {
      let time = this.leftTime
      this.tid = setInterval(() => {
        if (time > 0) {
          time --
          this.time = this.formatTime(time)
        } else {
          clearInterval(this.tid)
        }
      }, 1000)
    },
    formatTime (time) {
      const minute = Math.floor(time / 60)
      const second = time % 60
      return `${minute < 10 ? '0' + minute : minute}:${second < 10 ? '0' + second : second}`
    }
  }
}
</script>

八、Vue倒計時取消

在某些業務場景中,我們可能需要在倒計時進行的過程中取消倒計時,我們可以在Vue倒計時組件中加入取消方法。以下是一個Vue倒計時組件的示例:


<template>
  <div class="countdown">
    {{ time }}
    <button @click="cancel">取消</button>
  </div>
</template>

<script>
export default {
  name: 'Countdown',
  props: {
    totalTime: {
      type: Number,
      required: true,
      default: 60
    },
    callback: {
      type: Function,
      required: false
    }
  },
  data () {
    return {
      time: ''
    }
  },
  created () {
    this.countDown()
  },
  methods: {
    countDown () {
      let time = this.totalTime
      this.tid = setInterval(() => {
        if (time > 0) {
          time --
          this.time = this.formatTime(time)
        } else {
          clearInterval(this.tid)
          if (this.callback) {
            this.callback()
          }
        }
      }, 1000)
    },
    formatTime (time) {
      const minute = Math.floor(time / 60)
      const second = time % 60
      return `${minute < 10 ? '0' + minute : minute}:${second < 10 ? '0' + second : second}`
    },
    cancel () {
      clearInterval(this.tid)
      // 如果還有其他回調,也可以在這裡執行
    }
  }
}
</script>

九、Vue倒計時按鈕組件

在許多業務

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/257823.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-15 12:47
下一篇 2024-12-15 12:47

相關推薦

  • 使用Vue實現前端AES加密並輸出為十六進制的方法

    在前端開發中,數據傳輸的安全性問題十分重要,其中一種保護數據安全的方式是加密。本文將會介紹如何使用Vue框架實現前端AES加密並將加密結果輸出為十六進制。 一、AES加密介紹 AE…

    編程 2025-04-29
  • Vue TS工程結構用法介紹

    在本篇文章中,我們將從多個方面對Vue TS工程結構進行詳細的闡述,涵蓋文件結構、路由配置、組件間通訊、狀態管理等內容,並給出對應的代碼示例。 一、文件結構 一個好的文件結構可以極…

    編程 2025-04-29
  • Vue3的vue-resource使用教程

    本文將從以下幾個方面詳細闡述Vue3如何使用vue-resource。 一、安裝Vue3和vue-resource 在使用vue-resource前,我們需要先安裝Vue3和vue…

    編程 2025-04-27
  • ThinkPHP6 + Vue.js: 不使用Fetch的數據請求方法

    本文將介紹如何在ThinkPHP6和Vue.js中進行數據請求,同時避免使用Fetch函數。 一、AJAX:XMLHttpRequest的基礎使用 在進行數據請求時,最基礎的方式就…

    編程 2025-04-27
  • Vue模擬按鍵按下

    本文將從以下幾個方面對Vue模擬按鍵按下進行詳細闡述: 一、Vue 模擬按鍵按下的場景 在前端開發中,我們常常需要模擬按鍵按下的場景,比如在表單中填寫內容後,按下“回車鍵”提交表單…

    編程 2025-04-27
  • 開發前端程序,Vue是否足夠?

    Vue是一個輕量級,高效,漸進式的JavaScript框架,用於構建Web界面。開發人員可以使用Vue輕鬆完成前端編程,開發響應式應用程序。然而,當涉及到需要更大的生態系統,或利用…

    編程 2025-04-27
  • 如何在Vue中點擊清除SetInterval

    在Vue中點擊清除SetInterval是常見的需求之一。本文將介紹如何在Vue中進行這個操作。 一、使用setInterval和clearInterval 在Vue中,使用set…

    編程 2025-04-27
  • Linux sync詳解

    一、sync概述 sync是Linux中一個非常重要的命令,它可以將文件系統緩存中的內容,強制寫入磁盤中。在執行sync之前,所有的文件系統更新將不會立即寫入磁盤,而是先緩存在內存…

    編程 2025-04-25
  • 神經網絡代碼詳解

    神經網絡作為一種人工智能技術,被廣泛應用於語音識別、圖像識別、自然語言處理等領域。而神經網絡的模型編寫,離不開代碼。本文將從多個方面詳細闡述神經網絡模型編寫的代碼技術。 一、神經網…

    編程 2025-04-25
  • Python輸入輸出詳解

    一、文件讀寫 Python中文件的讀寫操作是必不可少的基本技能之一。讀寫文件分別使用open()函數中的’r’和’w’參數,讀取文件…

    編程 2025-04-25

發表回復

登錄後才能評論