vue實現點擊按鈕倒計時
更新時間:2022年07月11日 09:03:35 作者:饑餓的帕尼尼
這篇文章主要為大家詳細介紹了vue實現點擊按鈕倒計時,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現點擊按鈕倒計時的具體代碼,供大家參考,具體內容如下
實現效果:
1.點擊開始按鈕啟動計時

2.點擊重置按鈕計時恢復到00:00:00
3.點擊暫停按鈕暫停計時
Vue代碼:
<template>
? <div>
? ? <div class="timeContainer">{{ time }}</div>
? ? <a-button style="margin-right: 20px" type="primary" @click="start"
? ? ? >開始</a-button
? ? >
? ? <a-button style="margin-right: 20px" type="primary" @click="reset"
? ? ? >重置</a-button
? ? >
? ? <a-button type="primary" @click="end">暫停</a-button>
? </div>
</template>
<script>
export default {
? data() {
? ? return {
? ? ? flag: null,
? ? ? hour: 0,
? ? ? minute: 0,
? ? ? second: 0,
? ? ? time: "00:00:00",
? ? };
? },
? methods: {
? ? //開始計時
? ? start() {
? ? ? this.flag = setInterval(() => {
? ? ? ? this.second = this.second + 1;
? ? ? ? if (this.second >= 60) {
? ? ? ? ? this.second = 0;
? ? ? ? ? this.minute = this.minute + 1;
? ? ? ? }
? ? ? ? if (this.minute >= 60) {
? ? ? ? ? this.minute = 0;
? ? ? ? ? this.hour = this.hour + 1;
? ? ? ? }
? ? ? ? this.time =
? ? ? ? ? this.complZero(this.hour) +
? ? ? ? ? ":" +
? ? ? ? ? this.complZero(this.minute) +
? ? ? ? ? ":" +
? ? ? ? ? this.complZero(this.second);
? ? ? }, 1000);
? ? },
? ? //重新計時
? ? reset() {
? ? ? window.clearInterval(this.flag);
? ? ? this.hour = 0;
? ? ? this.minute = 0;
? ? ? this.second = 0;
? ? ? this.time = "00:00:00";
? ? },
? ? //暫停計時
? ? end() {
? ? ? this.flag = clearInterval(this.flag);
? ? },
? ? //補零
? ? complZero(n) {
? ? ? return n < 10 ? "0" + n : "" + n;
? ? },
? },
};
</script>
<style>
.timeContainer {
? font-size: 40px;
? margin-bottom: 10px;
}
</style>以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
vue-music 使用better-scroll遇到輪播圖不能自動輪播問題
根據vue-music視頻中slider組建的使用,當安裝新版本的better-scroll,輪播組件,不能正常輪播。如何解決這個問題呢,下面小編給大家?guī)砹藇ue-music 使用better-scroll遇到輪播圖不能自動輪播問題,感興趣的朋友一起看看吧2018-12-12

