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 相关文章推荐
cssQuery()的下载与使用方法
Jan 12 Javascript
自定义一个jquery插件[鼠标悬浮时候 出现说明label]
Jun 27 Javascript
基于jQuery捕获超链接事件进行局部刷新代码
May 10 Javascript
js利用数组length属性清空和截短数组的小例子
Jan 15 Javascript
setInterval计时器不准的问题解决方法
May 08 Javascript
在Node.js应用中读写Redis数据库的简单方法
Jun 30 Javascript
JS实现的文字与图片定时切换效果代码
Oct 06 Javascript
BootstrapTable与KnockoutJS相结合实现增删改查功能【二】
May 10 Javascript
VUE元素的隐藏和显示(v-show指令)
Jun 23 Javascript
用vue2.0实现点击选中active其他选项互斥的效果
Apr 12 Javascript
在Vue环境下利用worker运行interval计时器的步骤
Aug 01 Javascript
微信小程序以ssm做后台开发的实现示例
Apr 08 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 cache类代码(php数据缓存类)
2010/04/15 PHP
php如何调用webservice应用介绍
2012/11/24 PHP
PHP设置图片文件上传大小的具体实现方法
2013/10/11 PHP
PHP读取txt文本文件并分页显示的方法
2015/03/11 PHP
对PHP依赖注入的理解实例分析
2016/10/09 PHP
php实现表单提交上传文件功能
2018/05/28 PHP
PHP实现Snowflake生成分布式唯一ID的方法示例
2020/08/30 PHP
jquery select(列表)的操作(取值/赋值)
2011/03/16 Javascript
一个简单的Ext.XTemplate的实例代码
2012/03/18 Javascript
js控制鼠标事件移动及移出效果显示
2014/10/19 Javascript
JavaScript正则表达式中的ignoreCase属性使用详解
2015/06/16 Javascript
javascript产生随机数方法汇总
2016/01/25 Javascript
javascript拖拽应用实例
2016/03/25 Javascript
jQuery模拟select实现下拉菜单功能
2016/06/20 Javascript
jQuery版AJAX简易封装代码
2016/09/14 Javascript
详解微信小程序入门五: wxml文件引用、模版、生命周期
2017/01/20 Javascript
微信小程序网络请求的封装与填坑之路
2017/04/01 Javascript
Vue.js上下滚动加载组件的实例代码
2017/07/17 Javascript
vue中created和mounted的区别浅析
2019/08/13 Javascript
javascript 构建模块化开发过程解析
2019/09/11 Javascript
Element Dialog对话框的使用示例
2020/07/26 Javascript
[04:52]2015国际邀请赛LGD战队晋级之路
2015/08/14 DOTA
[01:03:00]DOTA2上海特级锦标赛A组败者赛 EHOME VS CDEC第一局
2016/02/25 DOTA
[01:13:08]2018DOTA2亚洲邀请赛4.6 淘汰赛 mineski vs LGD 第二场
2018/04/10 DOTA
Python写的英文字符大小写转换代码示例
2015/03/06 Python
Perl中著名的Schwartzian转换问题解决实现
2015/06/02 Python
Python判断两个对象相等的原理
2017/12/12 Python
python 使用 requests 模块发送http请求 的方法
2018/12/09 Python
Python datetime 如何处理时区信息
2020/09/02 Python
小学教师的个人自我鉴定
2013/10/24 职场文书
什么是岗位职责
2013/11/12 职场文书
优秀共产党员先进事迹材料
2014/05/06 职场文书
个人工作年终总结
2015/03/09 职场文书
创业计划书之甜品店
2019/09/18 职场文书
JavaScript声明变量和数据类型的转换
2022/04/12 Javascript
如何Tomcat中使用ipv6地址
2022/05/06 Servers