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实例教程(19) 使用HoTMetal(4)
Dec 23 Javascript
jQuery中事件对象e的事件冒泡用法示例介绍
Apr 25 Javascript
JS实现图片无间断滚动代码汇总
Jul 30 Javascript
jQuery源码分析之jQuery.fn.each与jQuery.each用法
Jan 23 Javascript
JavaScript进阶练习及简单实例分析
Jun 03 Javascript
Vuex之理解state的用法实例
Apr 19 Javascript
Require.js的基本用法详解
Jul 03 Javascript
jQuery简单实现向列表动态添加新元素的方法示例
Dec 25 jQuery
详解angular如何调用HTML字符串的方法
Jun 30 Javascript
微信小程序简单的canvas裁剪图片功能详解
Jul 12 Javascript
JavaScript定时器常见用法实例分析
Nov 15 Javascript
解决vant中 tab栏遇到的坑 van-tabs
Nov 04 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
PHP5.2下chunk_split()函数整数溢出漏洞 分析
2007/06/06 PHP
PHP实现对数组分页处理实例详解
2017/02/07 PHP
Zend Framework基于Command命令行建立ZF项目的方法
2017/02/18 PHP
让textarea自动调整大小的js代码
2011/04/12 Javascript
页面定时刷新(1秒刷新一次)
2013/11/22 Javascript
深入理解JS DOM事件机制
2016/08/06 Javascript
AngularJS 整理一些优化的小技巧
2016/08/18 Javascript
Bootstrap框架安装使用详解
2017/01/21 Javascript
jquery.validate表单验证插件使用详解
2017/06/21 jQuery
使用html+js+css 实现页面轮播图效果(实例讲解)
2017/09/21 Javascript
浅谈vue-router路由切换 组件重用挖下的坑
2019/11/01 Javascript
JS原型对象操作实例分析
2020/06/06 Javascript
vue-cli4项目开启eslint保存时自动格式问题
2020/07/13 Javascript
原生js实现购物车
2020/09/23 Javascript
[08:54]DOTA2-DPC中国联赛 正赛 Aster vs LBZS 选手采访
2021/03/11 DOTA
Python聚类算法之DBSACN实例分析
2015/11/20 Python
python opencv实现任意角度的透视变换实例代码
2018/01/12 Python
windows下搭建python scrapy爬虫框架步骤
2018/12/23 Python
Django 反向生成url实例详解
2019/07/30 Python
浅谈Python访问MySQL的正确姿势
2020/01/07 Python
Pandas把dataframe或series转换成list的方法
2020/06/14 Python
使用Python pip怎么升级pip
2020/08/11 Python
Python requests接口测试实现代码
2020/09/08 Python
详解CSS3 用border写 空心三角箭头 (两种写法)
2017/09/29 HTML / CSS
从当地商店送来的杂货:Instacart
2018/08/19 全球购物
电影T恤、80年代T恤和80年代服装:TV Store Online
2020/01/05 全球购物
ktv收银员岗位职责
2013/12/16 职场文书
六十大寿答谢词
2014/01/12 职场文书
模具数控专业自荐信
2014/01/27 职场文书
函授本科个人自我鉴定
2014/03/25 职场文书
建材投资建议书
2014/05/16 职场文书
学生检讨书如何写
2014/10/30 职场文书
工商局个人工作总结
2015/03/03 职场文书
环卫处个人工作总结
2015/03/04 职场文书
2015年幼儿园国庆节活动总结
2015/07/30 职场文书
python批量更改目录名/文件名的方法
2021/04/18 Python