- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > JavaScript >
- Vue計時(shí)器的用法詳解
本文實(shí)例為大家分享了Vue實(shí)現計時(shí)器的具體代碼,供大家參考,具體內容如下
1、初始值為0,點(diǎn)擊【加】按鈕,數字自+1;連續點(diǎn)擊【加】,不影響數字+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. 導入Vue包 --> <script src="./lib/vue-2.4.0.js"></script> </head> <body> <!-- 2. 創(chuàng )建一個(gè)要控制的區域 --> <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() { // 計時(shí)器正在進(jìn)行中,退出函數 if (this.intervalId != null) { return }; // 計時(shí)器為空,操作 this.intervalId = setInterval(() => { this.count += 1 }, 400) }, // 停止定時(shí)器 stop() { clearInterval(this.intervalId)//清除計時(shí)器 this.intervalId = null;//設置為null } } }) </script> </body> </html>
之前小編收藏了一個(gè)開(kāi)始計時(shí)的組件,這個(gè)組件可直接引入到項目中使用,謝謝原作者分享。
<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ì)热?,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站