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 相关文章推荐
JQuery 应用 JQuery.groupTable.js
Dec 15 Javascript
详解JavaScript中Date.UTC()方法的使用
Jun 12 Javascript
JS阻止事件冒泡行为和闭包的方法
Jun 16 Javascript
json实现添加、遍历与删除属性的方法
Jun 17 Javascript
详解RequireJS按需加载样式文件
Apr 12 Javascript
angularjs1.5 组件内用函数向外传值的实例
Sep 30 Javascript
Vue中使用方法、计算属性或观察者的方法实例详解
Oct 31 Javascript
bootstrap下拉分页样式 带跳转页码
Dec 29 Javascript
VUE搭建手机商城心得和遇到的坑
Feb 21 Javascript
JavaScript函数的4种调用方法实例分析
Mar 05 Javascript
关于vue里页面的缓存详解
Nov 04 Javascript
JavaScript 生成唯一ID的几种方式
Feb 19 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 flv视频时间获取函数
2010/06/29 PHP
PHP5中GD库生成图形验证码(有汉字)
2013/07/28 PHP
php格式化金额函数分享
2015/02/02 PHP
PHP常用的小程序代码段
2015/11/14 PHP
PHP5.6读写excel表格文件操作示例
2019/02/26 PHP
JavaScript Undefined,Null类型和NaN值区别
2008/10/22 Javascript
Javascript中Eval函数的使用
2010/03/23 Javascript
JavaScript截取字符串的Slice、Substring、Substr函数详解和比较
2014/03/20 Javascript
javascript中加号(+)操作符的一些神奇作用
2014/06/06 Javascript
JavaScript实现N皇后问题算法谜题解答
2014/12/29 Javascript
jQuery实现灰蓝风格标准二级下拉菜单效果代码
2015/08/31 Javascript
jQuery实现仿百度首页滑动伸缩展开的添加服务效果代码
2015/09/09 Javascript
js改变透明度实现轮播图的算法
2020/08/24 Javascript
理解AngularJs篇:30分钟快速掌握AngularJs
2016/12/23 Javascript
Vue.js实现一个漂亮、灵活、可复用的提示组件示例
2017/03/17 Javascript
JavaScript-定时器0~9抽奖系统详解(代码)
2017/08/16 Javascript
bing Map 在vue项目中的使用详解
2018/04/09 Javascript
浅谈Webpack打包优化技巧
2018/06/12 Javascript
微信小程序实现手势滑动效果
2019/08/26 Javascript
Chrome插件开发系列一:弹窗终结者开发实战
2020/10/02 Javascript
[48:05]2018DOTA2亚洲邀请赛 3.31 小组赛 B组 VGJ.T vs VP
2018/03/31 DOTA
在Python中操作时间之tzset()方法的使用教程
2015/05/22 Python
Python操作SQLite数据库的方法详解【导入,创建,游标,增删改查等】
2017/07/11 Python
python使用Flask操作mysql实现登录功能
2018/05/14 Python
Python中出现IndentationError:unindent does not match any outer indentation level错误的解决方法
2020/04/18 Python
Python实现的调用C语言函数功能简单实例
2019/03/13 Python
python实现nao机器人身体躯干和腿部动作操作
2019/04/29 Python
Python实现将字符串的首字母变为大写,其余都变为小写的方法
2019/06/11 Python
Python 网络编程之UDP发送接收数据功能示例【基于socket套接字】
2019/10/11 Python
python实现手势识别的示例(入门)
2020/04/15 Python
Python解析微信dat文件的方法
2020/11/30 Python
Python 无限级分类树状结构生成算法的实现
2021/01/21 Python
SHEIN台湾:购买最新流行女装服饰
2019/05/18 全球购物
护理学专业推荐信
2013/12/03 职场文书
班级旅游计划书
2014/05/03 职场文书
创业计划书之养殖业
2019/10/11 职场文书