最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2019年05月10日 08:41:40   作者:zhy前端攻城獅  
這篇文章主要介紹了Angular2實現(xiàn)的秒表及改良版,結(jié)合實例形式分析 Angular2實現(xiàn)秒表功能的原理、操作技巧與相關(guān)注意事項,需要的朋友可以參考下

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

初版

代碼:

export class Watches {
  id: number;
  str: string;
}
export let watcheList: Watches[] = [{
  id: 0, str: '123456'
}, {
  id: 1, str: '564822'
}]
//watchList 是一個靜態(tài)類
watchList = watcheList;
watchStr: string;
//判斷是否是第一次點擊startWatch
num: number = 0;
//分 秒 毫秒
minute: number = 0;
second: number = 0;
millisecond: number = 0;
//臨時變量 存儲計次時的時間,后加入watcheList數(shù)組
temp= {
 id: 0,
 str: '0'
};
//定時器的名字
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是,在前面加一個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)該為計次
   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);
  }
 }
}

原理:

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

缺點:

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

改良版

代碼:

// 秒表
export class Watches {
  id: number;
  value: number;
}
export let watcheList: Watches[] = []
export class StopwatchComponent {
 //導(dǎo)入的靜態(tài)類
 watchList = watcheList;
 //臨時變量,用來存貯計次時的時間
 temp: number;
 //判斷startWatch是第一次開始,還是計次
 num: number = 0;
 //開始時間
 startTime: number;
 //當(dāng)前時間
 nowTime: number;
 //時間差
 timerRunner: number = 0;
 //interval函數(shù)的名稱
 inter: any;
 constructor() { }
 resetWatch() {
  //清零
  this.timerRunner = 0;
  this.watchList.length = 0;
 }
 startWatch(event) {
  this.temp = this.timerRunner;
  //開始計時的時間
  this.startTime = Date.now();
  this.num = this.num + 1;
  if (this.num > 1) {
   //當(dāng)前狀態(tài)為計時,將計時的數(shù)據(jù)加入進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擊startWatch時,獲取當(dāng)前時間作為開始時間,并每43ms觸發(fā)定時器,獲取最新時間。時間差則為最新時間減去開始時間

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

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

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

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

相關(guān)文章

  • AngularJS表格添加序號的方法

    AngularJS表格添加序號的方法

    這篇文章主要介紹了AngularJS表格添加序號的方法,涉及AngularJS表格的遍歷及序號添加實現(xiàn)技巧,需要的朋友可以參考下
    2017-03-03
  • 淺談關(guān)于angularJs中使用$.ajax的注意點

    淺談關(guān)于angularJs中使用$.ajax的注意點

    本篇文章主要介紹了關(guān)于angularJs中使用$.ajax的注意點,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Angular中的Promise對象($q介紹)

    Angular中的Promise對象($q介紹)

    這篇文章主要介紹了Angular中的Promise對象($q介紹),本文講解了Promise模式、Q Promise的基本用法、AngularJs中的$q.defferd等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • angular + express 實現(xiàn)websocket通信

    angular + express 實現(xiàn)websocket通信

    最近需要實現(xiàn)一個功能,后端通過TCP協(xié)議連接雷達硬件的控制器,前端通過websocket連接后端,當(dāng)控制器觸發(fā)消息的時候,把信息通知給所以前端,本文給的大家講解angular + express 實現(xiàn)websocket通信的思路,感興趣的朋友一起看看吧
    2023-09-09
  • AngualrJs清除定時器遇到的坑

    AngualrJs清除定時器遇到的坑

    這篇文章主要介紹了AngualrJs清除定時器遇到的坑,需要的朋友可以參考下
    2017-10-10
  • Angular實現(xiàn)表單驗證功能

    Angular實現(xiàn)表單驗證功能

    這篇文章主要為大家詳細介紹了Angular實現(xiàn)表單驗證功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Angular之toDoList的實現(xiàn)代碼示例

    Angular之toDoList的實現(xiàn)代碼示例

    本篇文章主要介紹了Angular之toDoList的實現(xiàn)代碼示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • 詳解Angularjs 自定義指令中的數(shù)據(jù)綁定

    詳解Angularjs 自定義指令中的數(shù)據(jù)綁定

    這篇文章主要介紹了Angularjs 自定義指令中的數(shù)據(jù)綁定,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • AngularJS動態(tài)綁定ng-options的ng-model實例代碼

    AngularJS動態(tài)綁定ng-options的ng-model實例代碼

    本篇文章主要介紹了AngularJS動態(tài)綁定ng-options的ng-model實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • AngularJS基于provider實現(xiàn)全局變量的讀取和賦值方法

    AngularJS基于provider實現(xiàn)全局變量的讀取和賦值方法

    這篇文章主要介紹了AngularJS基于provider實現(xiàn)全局變量的讀取和賦值方法,結(jié)合實例形式分析了AngularJS全局變量的聲明、賦值、讀取等相關(guān)使用技巧,需要的朋友可以參考下
    2017-06-06

最新評論

黑水县| 修武县| 博野县| 鄄城县| 淮安市| 称多县| 诸城市| 客服| 永顺县| 根河市| 万宁市| 铁力市| 金塔县| 宜兴市| 延安市| 仪征市| 林周县| 宁安市| 黎城县| 淮阳县| 宿松县| 宣恩县| 纳雍县| 广宗县| 乌兰县| 武定县| 金塔县| 台江县| 思茅市| 大同市| 临西县| 石嘴山市| 甘南县| 文登市| 漯河市| 寿阳县| 佛山市| 来凤县| 融水| 股票| 宁陕县|