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


Posted in Javascript onMay 10, 2019

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

初版

代码:

export class Watches {
  id: number;
  str: string;
}
export let watcheList: Watches[] = [{
  id: 0, str: '123456'
}, {
  id: 1, str: '564822'
}]
//watchList 是一个静态类
watchList = watcheList;
watchStr: string;
//判断是否是第一次点击startWatch
num: number = 0;
//分 秒 毫秒
minute: number = 0;
second: number = 0;
millisecond: number = 0;
//临时变量 存储计次时的时间,后加入watcheList数组
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,调用该函数,所以增加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;
 }
//当小于10是,在前面加一个0,形式则变为00: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) {
   //该状态应该为计次
   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函数里面,定义了一个变量毫秒millisecond,每隔43ms调用timer函数,所以millisecond每次增加43,而后1000ms之后seond增加1,60s之后,minute增加1.

缺点:

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

改良版

代码:

// 秒表
export class Watches {
  id: number;
  value: number;
}
export let watcheList: Watches[] = []
export class StopwatchComponent {
 //导入的静态类
 watchList = watcheList;
 //临时变量,用来存贮计次时的时间
 temp: number;
 //判断startWatch是第一次开始,还是计次
 num: number = 0;
 //开始时间
 startTime: number;
 //当前时间
 nowTime: number;
 //时间差
 timerRunner: number = 0;
 //interval函数的名称
 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) {
   //当前状态为计时,将计时的数据加入进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);
  }
 }
}

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

Javascript 相关文章推荐
JavaScript 闭包深入理解(closure)
May 27 Javascript
javascript:void(0)使用探讨
Aug 27 Javascript
iframe里的页面禁止右键事件的方法
Jun 10 Javascript
JavaScript实现生成GUID(全局统一标识符)
Sep 05 Javascript
js和jQuery设置Opacity半透明 兼容IE6
May 24 Javascript
JavaScript原生编写《飞机大战坦克》游戏完整实例
Jan 04 Javascript
红黑树的插入详解及Javascript实现方法示例
Mar 26 Javascript
Vue在 Nuxt.js 中重定向 404 页面的方法
Apr 23 Javascript
Vue 中文本内容超出规定行数后展开收起的处理的实现方法
Apr 28 Javascript
JS异步处理的进化史深入讲解
Aug 25 Javascript
axios实现简单文件上传功能
Sep 25 Javascript
JS typeof fn === 'function' && fn()详解
Aug 22 Javascript
node中IO以及定时器优先级详解
May 10 #Javascript
使用Node.js写一个代码生成器的方法步骤
May 10 #Javascript
Easyui 去除jquery-easui tab页div自带滚动条的方法
May 10 #jQuery
使用vue脚手架(vue-cli)搭建一个项目详解
May 09 #Javascript
Node.js实现用户评论社区功能(体验前后端开发的乐趣)
May 09 #Javascript
微信小程序中显示倒计时代码实例
May 09 #Javascript
微信小程序日历弹窗选择器代码实例
May 09 #Javascript
You might like
FleaPHP的安全设置方法
2008/09/15 PHP
比较完整的微信开发php代码
2016/08/02 PHP
PHP isset()及empty()用法区别详解
2020/08/29 PHP
jQuery EasyUI API 中文文档 - TimeSpinner时间微调器
2011/10/23 Javascript
js二维数组排序的简单示例代码
2014/01/24 Javascript
jQuery+PHP实现动态数字展示特效
2015/03/14 Javascript
jquery操作select元素和option的实例代码
2016/02/03 Javascript
jQuery绑定事件-多种实现方式总结
2016/05/09 Javascript
基于Node的React图片上传组件实现实例代码
2017/05/10 Javascript
浅谈JS对html标签的属性的干预以及对CSS样式表属性的干预
2017/06/25 Javascript
angularjs结合html5实现拖拽功能
2018/06/25 Javascript
vue+axios 前端实现登录拦截的两种方式(路由拦截、http拦截)
2018/10/24 Javascript
跟混乱的页面弹窗说再见
2019/04/11 Javascript
使用Phantomjs和Node完成网页的截屏快照的方法
2019/07/16 Javascript
layui导出所有数据的例子
2019/09/10 Javascript
微信小程序中使用 async/await的方法实例分析
2020/05/06 Javascript
让你30分钟快速掌握vue3教程
2020/10/26 Javascript
详解Python list 与 NumPy.ndarry 切片之间的对比
2017/07/24 Python
python简单实例训练(21~30)
2017/11/15 Python
pandas数据框,统计某列数据对应的个数方法
2018/04/11 Python
python设定并获取socket超时时间的方法
2019/01/12 Python
Python测试线程应用程序过程解析
2019/12/31 Python
在Tensorflow中实现leakyRelu操作详解(高效)
2020/06/30 Python
tensorflow基于CNN实战mnist手写识别(小白必看)
2020/07/20 Python
Pandas中两个dataframe的交集和差集的示例代码
2020/12/13 Python
css3 transform导致子元素固定定位变成绝对定位的方法
2020/03/06 HTML / CSS
Tomcat Mysql datasource数据源配置
2015/12/28 面试题
计算s=f(f(-1.4))的值
2014/05/06 面试题
应届生求职信写作技巧
2013/10/24 职场文书
毕业生个人投资创业计划书
2014/01/04 职场文书
创意活动策划书
2014/01/15 职场文书
小学生家长评语集锦
2014/01/30 职场文书
社区消防工作实施方案
2014/03/21 职场文书
高中历史教学反思
2016/02/19 职场文书
导游词之南京汤山温泉
2019/11/26 职场文书
MySQL图形化管理工具Navicat安装步骤
2021/12/04 MySQL