Vue計(jì)時(shí)器的用法詳解
本文實(shí)例為大家分享了Vue實(shí)現(xiàn)計(jì)時(shí)器的具體代碼,供大家參考,具體內(nèi)容如下

功能簡(jiǎn)介:
1、初始值為0,點(diǎn)擊【加】按鈕,數(shù)字自+1;連續(xù)點(diǎn)擊【加】,不影響數(shù)字+1
2、點(diǎn)擊【?!堪粹o,停止+1
源碼:
<!DOCTYPE html>
<html add="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<!-- 1. 導(dǎo)入Vue包 -->
<script src="./lib/vue-2.4.0.js"></script>
</head>
<body>
<!-- 2. 創(chuàng)建一個(gè)要控制的區(qū)域 -->
<div id="app">
<input type="button" value="加" @click="add">
<input type="button" value="停" @click="stop">
<h4>{{ count }}</h4>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
count: 0,
intervalId: null
},
methods: {
add() {
// 計(jì)時(shí)器正在進(jìn)行中,退出函數(shù)
if (this.intervalId != null) {
return
};
// 計(jì)時(shí)器為空,操作
this.intervalId = setInterval(() => {
this.count += 1
}, 400)
},
// 停止定時(shí)器
stop() {
clearInterval(this.intervalId)//清除計(jì)時(shí)器
this.intervalId = null;//設(shè)置為null
}
}
})
</script>
</body>
</html>
之前小編收藏了一個(gè)開始計(jì)時(shí)的組件,這個(gè)組件可直接引入到項(xiàng)目中使用,謝謝原作者分享。
<template>
<div class="timer">
<div ref="startTimer"></div>
</div>
</template>
<script>
export default {
name: 'Timer',
data () {
return {
timer: "",
content: "",
hour: 0,
minutes: 0,
seconds: 0
}
},
created () {
this.timer = setInterval(this.startTimer, 1000);
},
destroyed () {
clearInterval(this.timer);
},
methods: {
startTimer () {
this.seconds += 1;
if (this.seconds >= 60) {
this.seconds = 0;
this.minute = this.minute + 1;
}
if (this.minute >= 60) {
this.minute = 0;
this.hour = this.hour + 1;
}
this.$refs.startTimer.innerHTML = (this.minutes < 10 ? '0' + this.minutes : this.minutes) + ':' + (this.seconds < 10 ? '0' + this.seconds : this.seconds);
}
}
}
</script>
<style>
</style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于Vue方法實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器
- 使用vue實(shí)現(xiàn)計(jì)時(shí)器功能
- vue.js實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器功能
- vue實(shí)現(xiàn)簡(jiǎn)易計(jì)時(shí)器組件
- vue計(jì)時(shí)器的實(shí)現(xiàn)方法
- 解決vue組件銷毀之后計(jì)時(shí)器繼續(xù)執(zhí)行的問題
- 在Vue環(huán)境下利用worker運(yùn)行interval計(jì)時(shí)器的步驟
- Vue 使用計(jì)時(shí)器實(shí)現(xiàn)跑馬燈效果的實(shí)例代碼
- vue 計(jì)時(shí)器組件的實(shí)現(xiàn)代碼
- Vue.js實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器應(yīng)用
相關(guān)文章
vue實(shí)現(xiàn)側(cè)邊欄導(dǎo)航效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)側(cè)邊欄導(dǎo)航效果,右側(cè)顯示對(duì)應(yīng)內(nèi)容,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
vue.js 使用v-if v-else發(fā)現(xiàn)沒有執(zhí)行解決辦法
這篇文章主要介紹了vue.js 使用v-if v-else發(fā)現(xiàn)沒有執(zhí)行解決辦法的相關(guān)資料,需要的朋友可以參考下2017-05-05
Vue解析剪切板圖片并實(shí)現(xiàn)發(fā)送功能
這篇文章主要介紹了Vue解析剪切板圖片并實(shí)現(xiàn)發(fā)送功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
用vue實(shí)現(xiàn)注冊(cè)頁(yè)效果?vue實(shí)現(xiàn)短信驗(yàn)證碼登錄
這篇文章主要為大家詳細(xì)介紹了用vue實(shí)現(xiàn)注冊(cè)頁(yè),短信驗(yàn)證碼登錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
vue vantUI tab切換時(shí) list組件不觸發(fā)load事件的問題及解決方法
這篇文章主要介紹了vue vantUI tab切換時(shí) list組件不觸發(fā)load事件的解決辦法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Vue中的ref作用詳解(實(shí)現(xiàn)DOM的聯(lián)動(dòng)操作)
這篇文章主要介紹了Vue中的ref作用詳解(實(shí)現(xiàn)DOM的聯(lián)動(dòng)操作),需要的朋友可以參考下2017-08-08
vue雙向數(shù)據(jù)綁定指令v-model的用法
這篇文章主要介紹了vue雙向數(shù)據(jù)綁定指令v-model的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

