使用vue實現(xiàn)計時器功能
本文實例為大家分享了vue實現(xiàn)計時器功能的具體代碼,供大家參考,具體內(nèi)容如下
首先我們要知道setTimeout和setInterval的區(qū)別
setTimeout只在指定時間后執(zhí)行一次,代碼如下:
<script> //定時器 異步運行 function hello(){ alert('hello'); } //使用方法名字執(zhí)行方法 var t1 = window.setTimeout(hello,1000); var t2 = window.setTimeout('hello()',3000);//使用字符串執(zhí)行方法 window.clearTimeout(t1);//去掉定時器 </script>
setInterval以指定時間為周期循環(huán)執(zhí)行,代碼如下:
//實時刷新時間單位為毫秒 setInterval(’refreshQuery()’,8000); /* 刷新查詢 */ function refreshQuery(){ $('#mainTable').datagrid(’reload’,null); }
一般情況下setTimeout用于延遲執(zhí)行某方法或功能,setInterval則一般用于刷新表單,對于一些表單的實時指定時間刷新同步
計時器HTML代碼
<div class='father'> <ul> <li>{{one}}<span>:</span></li> <li>{{two}}<span>:</span></li> <li>{{three}}</li> </ul> <el-button type='primary' @click='startHandler'>開始</el-button> <el-button type='primary' @click='endHandler'>暫停</el-button></div>
JAVASCRIPT代碼
<script>export default { name: ’HelloWorld’, data(){ return { flag: null, one : ’00’, // 時 two : ’00’, // 分 three : ’00’, // 秒 abc : 0, // 秒的計數(shù) cde : 0, // 分的計數(shù) efg : 0, // 時的計數(shù) } }, props: { msg: String }, mounted() { }, methods:{ // 開始計時 startHandler(){ this.flag = setInterval(()=>{ if(this.three === 60 || this.three === ’60’){ this.three = ’00’; this.abc = 0; if(this.two === 60 || this.two === ’60’){ this.two = ’00’; this.cde = 0; if(this.efg+1 <= 9){ this.efg++; this.one = ’0’ + this.efg; }else{ this.efg++; this.one = this.efg; } }else{ if(this.cde+1 <= 9){ this.cde++; this.two = ’0’ + this.cde; }else{ this.cde++; this.two = this.cde; } } }else{ if(this.abc+1 <= 9){ this.abc++; this.three = ’0’ + this.abc; }else{ this.abc++; this.three=this.abc; } } },100) }, // 暫停計時 endHandler(){ this.flag = clearInterval(this.flag) } }}</script>
效果如下:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. React+umi+typeScript創(chuàng)建項目的過程2. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析3. 無線標記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁4. SharePoint Server 2019新特性介紹5. .Net core 的熱插拔機制的深入探索及卸載問題求救指南6. html清除浮動的6種方法示例7. 讀大數(shù)據(jù)量的XML文件的讀取問題8. XML入門的常見問題(二)9. ASP中常用的22個FSO文件操作函數(shù)整理10. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp
