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去掉字符串里的所有空格
Feb 08 Javascript
(jQuery,mootools,dojo)使用适合自己的编程别名命名
Sep 14 Javascript
javascript正则表达式中参数g(全局)的作用
Nov 11 Javascript
深入探寻javascript定时器
Jan 02 Javascript
ECMAScript6函数剩余参数(Rest Parameters)
Jun 12 Javascript
JavaScript模板引擎用法实例
Jul 10 Javascript
使用递归遍历对象获得value值的实现方法
Jun 14 Javascript
利用require.js与angular搭建spa应用的方法实例
Jul 19 Javascript
node简单实现一个更改头像功能的示例
Dec 29 Javascript
VUE预渲染及遇到的坑
Sep 03 Javascript
Element-ui中元素滚动时el-option超出元素区域的问题
May 30 Javascript
小程序实现筛子抽奖
May 26 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
PHP Google的translate API代码
2008/12/10 PHP
PHP 变量的定义方法
2010/01/26 PHP
php小技巧 把数组的键和值交换形成了新的数组,查找值取得键
2011/06/02 PHP
实用的简单PHP分页集合包括使用方法
2013/10/21 PHP
destoon实现调用热门关键字的方法
2014/07/15 PHP
php实现判断访问来路是否为搜索引擎机器人的方法
2015/04/15 PHP
UTF-8正则表达式如何匹配汉字
2015/08/03 PHP
PHP语法小结之基础和变量
2015/11/22 PHP
PHP面向对象中new self()与 new static()的区别浅析
2017/08/17 PHP
JavaScript Distilled 基础知识与函数
2010/04/07 Javascript
通过Jquery遍历Json的两种数据结构的实现代码
2011/01/19 Javascript
jquery 获取标签名(tagName)示例代码
2013/07/11 Javascript
解析jQuery的三种bind/One/Live事件绑定使用方法
2013/12/30 Javascript
Select标签下拉列表二级联动级联实例代码
2014/02/07 Javascript
js获得页面的高度和宽度的方法
2014/02/23 Javascript
jQuery中extend()和fn.extend()方法详解
2015/06/03 Javascript
Node.js中使用jQuery的做法
2016/08/17 Javascript
JavaScript 网页中实现一个计算当年还剩多少时间的倒数计时程序
2017/01/25 Javascript
canvas绘制七巧板
2017/02/03 Javascript
原生node.js案例--前后台交互
2017/02/20 Javascript
vue+webpack实现异步加载三种用法示例详解
2018/04/24 Javascript
基于vue-cli搭建多模块且各模块独立打包的项目
2019/06/12 Javascript
Vue Router中应用中间件的方法
2020/08/06 Javascript
[40:16]TFT vs Mski Supermajor小组赛C组 BO3 第二场 6.3
2018/06/04 DOTA
[01:08:29]DOTA2-DPC中国联赛定级赛 RNG vs Aster BO3第一场 1月9日
2021/03/11 DOTA
Python中exit、return、sys.exit()等使用实例和区别
2015/05/28 Python
Python计算斗牛游戏概率算法实例分析
2017/09/26 Python
对Python中range()函数和list的比较
2018/04/19 Python
解决phantomjs截图失败,phantom.exit位置的问题
2018/05/17 Python
Python内置加密模块用法解析
2019/11/25 Python
什么是python的列表推导式
2020/05/26 Python
工商管理实习自我鉴定
2013/09/28 职场文书
六查六看自查材料
2014/02/17 职场文书
2014乡党委副书记党建工作汇报材料
2014/11/02 职场文书
Windows10下安装MySQL8
2021/04/06 MySQL
Html5生成验证码的示例代码
2021/05/10 Javascript