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脚本实现Web页面信息交互
Dec 21 Javascript
一个可以显示阴历的JS代码
Mar 05 Javascript
纯javascript模仿微信打飞机小游戏
Aug 20 Javascript
JS根据浏览器窗口大小实时动态改变网页文字大小的方法
Feb 25 Javascript
js友好的时间返回函数
Aug 24 Javascript
用headjs来管理和加载js 提高网站加载速度
Nov 29 Javascript
jquery ui sortable拖拽后保存位置
Apr 27 jQuery
详解使用Typescript开发node.js项目(简单的环境配置)
Oct 09 Javascript
vue2.0 自定义 饼状图 (Echarts)组件的方法
Mar 02 Javascript
使用vue制作探探滑动堆叠组件的实例代码
Mar 07 Javascript
React 实现拖拽功能的示例代码
Jan 06 Javascript
vue 实现element-ui中的加载中状态
Nov 11 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迭代器的内部执行过程详解
2013/11/12 PHP
php通过array_unshift函数添加多个变量到数组前端的方法
2015/03/18 PHP
PHP中返回引用类型的方法
2015/04/03 PHP
浅谈php中fopen不能创建中文文件名文件的问题
2017/02/06 PHP
PHP date_default_timezone_set()设置时区操作实例分析
2020/05/16 PHP
Jquery插件写法笔记整理
2012/09/06 Javascript
Javascript遍历Html Table示例(包括内容和属性值)
2014/07/08 Javascript
JavaScript+html5 canvas绘制渐变区域完整实例
2016/01/26 Javascript
jQuery动态修改字体大小的方法【测试可用】
2016/09/09 Javascript
利用fecha进行JS日期处理
2016/11/21 Javascript
对vue中methods互相调用的方法详解
2018/08/30 Javascript
Vue弹出菜单功能的实现代码
2018/09/12 Javascript
详解mpvue小程序中怎么引入iconfont字体图标
2018/10/01 Javascript
vuex 中插件的编写案例解析
2019/06/10 Javascript
NodeJs 实现简单WebSocket即时通讯的示例代码
2019/08/05 NodeJs
vue-router的钩子函数用法实例分析
2019/10/26 Javascript
vue中使用rem布局代码详解
2019/10/30 Javascript
Node.js操作MongoDB数据库实例分析
2020/01/19 Javascript
js数组中去除重复值的几种方法
2020/08/03 Javascript
JavaScript实现弹出窗口效果
2020/12/09 Javascript
[01:18:35]DOTA2-DPC中国联赛 正赛 Elephant vs LBZS BO3 第一场 1月29日
2021/03/11 DOTA
Python常见数据结构详解
2014/07/24 Python
利用python实现微信头像加红色数字功能
2018/03/26 Python
在python中利用opencv简单做图片比对的方法
2019/01/24 Python
python能否java成为主流语言吗
2020/06/22 Python
python字典的值可以修改吗
2020/06/29 Python
解决python pandas读取excel中多个不同sheet表格存在的问题
2020/07/14 Python
Python使用lambda抛出异常实现方法解析
2020/08/20 Python
Python3.8.2安装包及安装教程图文详解(附安装包)
2020/11/28 Python
CSS3利用text-shadow属性实现多种效果的文字样式展现方法
2016/08/25 HTML / CSS
自主招生自荐书
2013/11/29 职场文书
酒店值班经理的工作职责范本
2014/02/18 职场文书
求职信模板标准格式范文
2014/02/23 职场文书
领导新年致辞2016
2015/07/29 职场文书
信息技术远程培训心得体会
2016/01/09 职场文书
祝福语集锦:朋友新店开业祝福语
2019/12/10 职场文书