溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Angular2實(shí)現(xiàn)的秒表及改良版示例

發(fā)布時(shí)間:2020-10-13 19:35:13 來(lái)源:腳本之家 閱讀:151 作者:zhy前端攻城獅 欄目:web開(kāi)發(fā)

本文實(shí)例講述了Angular2實(shí)現(xiàn)的秒表及改良版。分享給大家供大家參考,具體如下:

初版

代碼:

export class Watches {
  id: number;
  str: string;
}
export let watcheList: Watches[] = [{
  id: 0, str: '123456'
}, {
  id: 1, str: '564822'
}]
//watchList 是一個(gè)靜態(tài)類(lèi)
watchList = watcheList;
watchStr: string;
//判斷是否是第一次點(diǎn)擊startWatch
num: number = 0;
//分 秒 毫秒
minute: number = 0;
second: number = 0;
millisecond: number = 0;
//臨時(shí)變量 存儲(chǔ)計(jì)次時(shí)的時(shí)間,后加入watcheList數(shù)組
temp= {
 id: 0,
 str: '0'
};
//定時(shí)器的名字
inter: any;
constructor() { }
 resetWatch() {
   //清零
  this.millisecond = 0;
  this.second = 0;
  this.minute = 0;
  this.temp.str = '000000';
  watcheList.length = 0;
 }
timer() {
  //每隔43ms,調(diào)用該函數(shù),所以增加43
 this.millisecond = this.millisecond + 43;
 if (this.millisecond >= 1000) {
  this.millisecond = 0;
  this.second = this.second + 1;
 }
 if (this.second >= 60) {
  this.second = 0;
  this.minute = this.minute + 1;
 }
//當(dāng)小于10是,在前面加一個(gè)0,形式則變?yōu)?0:00:00
 this.watchStr = (this.minute > 10 ? this.minute : '0' + this.minute) + ':'
  + (this.second > 10 ? this.second : '0' + this.second) + ':'
  + (this.millisecond > 10 ? this.millisecond : '0' + this.millisecond);
}
 startWatch(event) {
  this.num = this.num + 1;
  if (this.num > 1) {
   //該狀態(tài)應(yīng)該為計(jì)次
   temp.id = this.watchList.length;
   temp.str = this.watchStr;
   this.watchList.push(temp);
  } else {
   this.inter = setInterval(() => {
    this.timer();
   }, 43);
  }
 }
 stopWatch(event) {
  this.num = 0;
  if (this.inter) {
   clearInterval(this.inter);
  }
 }
}

原理:

在計(jì)時(shí)器timer函數(shù)里面,定義了一個(gè)變量毫秒millisecond,每隔43ms調(diào)用timer函數(shù),所以millisecond每次增加43,而后1000ms之后seond增加1,60s之后,minute增加1.

缺點(diǎn):

函數(shù)的運(yùn)行時(shí)間不可控,所以毫秒的增加不準(zhǔn)確。

改良版

代碼:

// 秒表
export class Watches {
  id: number;
  value: number;
}
export let watcheList: Watches[] = []
export class StopwatchComponent {
 //導(dǎo)入的靜態(tài)類(lèi)
 watchList = watcheList;
 //臨時(shí)變量,用來(lái)存貯計(jì)次時(shí)的時(shí)間
 temp: number;
 //判斷startWatch是第一次開(kāi)始,還是計(jì)次
 num: number = 0;
 //開(kāi)始時(shí)間
 startTime: number;
 //當(dāng)前時(shí)間
 nowTime: number;
 //時(shí)間差
 timerRunner: number = 0;
 //interval函數(shù)的名稱(chēng)
 inter: any;
 constructor() { }
 resetWatch() {
  //清零
  this.timerRunner = 0;
  this.watchList.length = 0;
 }
 startWatch(event) {
  this.temp = this.timerRunner;
  //開(kāi)始計(jì)時(shí)的時(shí)間
  this.startTime = Date.now();
  this.num = this.num + 1;
  if (this.num > 1) {
   //當(dāng)前狀態(tài)為計(jì)時(shí),將計(jì)時(shí)的數(shù)據(jù)加入進(jìn)watchList
   let watchObj: Watches = {
    id: 0,
    value: 0
   }
   watchObj.id = this.watchList.length;
   watchObj.value = this.timerRunner;
   this.watchList.push(watchObj);
  } else {
   this.inter = setInterval(() => {
    this.nowTime = Date.now();
    this.timerRunner = this.temp + this.nowTime - this.startTime;
   }, 43);
  }
 }
 stopWatch(event) {
  this.num = 0;
  if (this.inter) {
   clearInterval(this.inter);
  }
 }
}

原理:當(dāng)?shù)谝淮吸c(diǎn)擊startWatch時(shí),獲取當(dāng)前時(shí)間作為開(kāi)始時(shí)間,并每43ms觸發(fā)定時(shí)器,獲取最新時(shí)間。時(shí)間差則為最新時(shí)間減去開(kāi)始時(shí)間

PS:這里再為打擊推薦一款功能相似的在線工具供大家參考:

在線秒表工具:
http://tools.jb51.net/bianmin/miaobiao

更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《AngularJS指令操作技巧總結(jié)》、《AngularJS入門(mén)與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》

希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI