Angular2实现的秒表及改良版示例

  

本文实例讲述了Angular2实现的秒表及改良版。分享给大家供大家参考,具体如下:

  

<强>初版

  

代码:         出口类手表{   id:数量;   str:字符串;   }   出口让watcheList:手表[]=[{   id: 0, str:“123456”   },{   id: 1、str:“564822”   })//观察名单中是一个静态类   观察名单中=watcheList;   watchStr:字符串;//判断是否是第一次点击startWatch   num:数量=0;//分秒毫秒   分钟:数量=0;   第二:数量=0;   毫秒:数量=0;//临时变量存储计次时的时间,后加入watcheList数组   temp={   id: 0,   str:“0”   };//定时器的名字   国际米兰:任何;   构造函数(){}   resetWatch () {//清零   this.millisecond=0;   这一点。第二个=0;   这一点。分钟=0;   this.temp。str=' 000000 ';   watcheList。长度=0;   }   计时器(){//每隔43女士,调用该函数,所以增加43   this.millisecond=this.millisecond + 43;   如果(this.millisecond祝辞=1000){   this.millisecond=0;   这一点。第二个=5诙? 1;   }   如果这一点。第二个祝辞=60){   这一点。第二个=0;   这一点。分钟=7种? 1;   }//当小于10是在前面加一个0,形式则变为就是   这一点。watchStr=(。分钟比;10 & # 63;这一点。分钟:‘0’+ this.minute) +“:”   +(这一点。第二比;10 & # 63;这一点。第二:‘0’+ this.second) +“:”   + (this.millisecond祝辞10 & # 63;this.millisecond:‘0’+ this.millisecond);   }   startWatch(事件){   这一点。num=um + 1;   如果这一点。num祝辞1){//该状态应该为计次   temp.id=this.watchList.length;   temp.str=this.watchStr;   this.watchList.push(临时);   其他}{   this.inter=setInterval(()=比;{   this.timer ();   },43);   }   }   秒表(事件){   这一点。num=0;   如果(this.inter) {   clearInterval (this.inter);   }   }   }      之前      

<强>原理:

  

在计时器定时器函数里面,定义了一个变量毫秒毫秒,每隔43调女士用计时器函数,所以毫秒每次增加43岁,而1000年后之女士后seond增加1,60年代之后,分钟增加1 .

  

缺点:   

函数的运行时间不可控,所以毫秒的增加不准确。

  

<强>改良版

  

代码:      //秒表   出口类手表{   id:数量;   价值:数量;   }   出口让watcheList:手表[]=[]   出口类StopwatchComponent {//导入的静态类   观察名单中=watcheList;//临时变量,用来存贮计次时的时间   临时:数量;//判断startWatch是第一次开始,还是计次   num:数量=0;//开始时间   开始时间:数量;//当前时间   nowTime:数量;//时间差   timerRunner:数量=0;//间隔函数的名称   国际米兰:任何;   构造函数(){}   resetWatch () {//清零   这一点。timerRunner=0;   this.watchList。长度=0;   }   startWatch(事件){   这一点。temp=this.timerRunner;//开始计时的时间   这一点。开始时间=Date.now ();   这一点。num=um + 1;   如果这一点。num祝辞1){//当前状态为计时,将计时的数据加入进观察名单中   让watchObj:手表={   id: 0,   值:0   }   watchObj。id=this.watchList.length;   watchObj。值=https://www.yisu.com/zixun/this.timerRunner;   this.watchList.push (watchObj);   其他}{   this.inter=setInterval (()=> {   这一点。nowTime=Date.now ();   这一点。timerRunner=A偈?。nowTime - this.startTime;   },43);   }   }   秒表(事件){   这一点。num=0;   如果(this.inter) {   clearInterval (this.inter);   }   }   }      之前      

原理:当第一次点击startWatch时,获取当前时间作为开始时间,并每43触女士发定时器,获取最新时间。时间差则为最新时间减去开始时间

  

  

<强> http://tools.jb51.net/bianmin/miaobiao
  

  

更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《AngularJS指令操作技巧总结》,《AngularJS入门与进阶教程》及《AngularJS MVC架构总结》

  

希望本文所述对大家AngularJS程序设计有所帮助。

Angular2实现的秒表及改良版示例