vue實現(xiàn)點擊按鈕倒計時
更新時間:2022年07月11日 09:03:35 作者:饑餓的帕尼尼
這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)點擊按鈕倒計時,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現(xiàn)點擊按鈕倒計時的具體代碼,供大家參考,具體內(nèi)容如下
實現(xiàn)效果:
1.點擊開始按鈕啟動計時
2.點擊重置按鈕計時恢復(fù)到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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue學(xué)習(xí)筆記進(jìn)階篇之多元素及多組件過渡
本篇文章主要介紹了Vue學(xué)習(xí)筆記進(jìn)階篇之多元素及多組件過渡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07vue實現(xiàn)鼠標(biāo)滑動預(yù)覽視頻封面組件示例詳解
這篇文章主要為大家介紹了vue實現(xiàn)鼠標(biāo)滑動預(yù)覽視頻封面組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07vue-music 使用better-scroll遇到輪播圖不能自動輪播問題
根據(jù)vue-music視頻中slider組建的使用,當(dāng)安裝新版本的better-scroll,輪播組件,不能正常輪播。如何解決這個問題呢,下面小編給大家?guī)砹藇ue-music 使用better-scroll遇到輪播圖不能自動輪播問題,感興趣的朋友一起看看吧2018-12-12