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 相关文章推荐
学习ExtJS border布局
Oct 08 Javascript
js展开闭合效果演示代码
Jul 24 Javascript
你有必要知道的25个JavaScript面试题
Dec 29 Javascript
判断是否存在子节点的实现代码
May 18 Javascript
javascript对浅拷贝和深拷贝的详解
Oct 14 Javascript
jQGrid Table操作列中点击【操作】按钮弹出按钮层的实现代码
Dec 05 Javascript
Vue.js结合bootstrap实现分页控件
Mar 10 Javascript
详解vue中使用axios对同一个接口连续请求导致返回数据混乱的问题
Nov 06 Javascript
Element Dialog对话框的使用示例
Jul 26 Javascript
vue实现移动端input上传视频、音频
Aug 18 Javascript
js 将多个对象合并成一个对象 assign方法的实现
Sep 24 Javascript
vue中使用echarts的示例
Jan 03 Vue.js
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新闻发布系统教程
2014/05/09 PHP
使用配置类定义Codeigniter全局变量
2014/06/12 PHP
php json转换成数组形式代码分享
2014/11/10 PHP
discuz图片顺序混乱解决方案
2015/07/29 PHP
PHP数字金额转换成中文大写显示
2019/01/05 PHP
jQuery实现的产品自动360度旋转展示特效源码分享
2015/08/21 Javascript
实例讲解js验证表单项是否为空的方法
2016/01/09 Javascript
浅谈JavaScript中小数和大整数的精度丢失
2016/05/31 Javascript
JavaScript中的Reflect对象详解(ES6新特性)
2016/07/22 Javascript
JS判断是否在微信浏览器打开的简单实例(推荐)
2016/08/24 Javascript
关于Vue.js一些问题和思考学习笔记(1)
2016/12/02 Javascript
jQuery实现form表单序列化转换为json对象功能示例
2018/05/23 jQuery
bootstrap下拉框动态赋值方法
2018/08/10 Javascript
微信小程序实现的动态设置导航栏标题功能示例
2019/01/31 Javascript
Layer.js实现表格溢出内容省略号显示,悬停显示全部的方法
2019/09/16 Javascript
vue 使用外部JS与调用原生API操作示例
2019/12/02 Javascript
[42:32]完美世界DOTA2联赛循环赛 Magma vs PXG BO2第二场 10.28
2020/10/28 DOTA
[38:38]完美世界DOTA2联赛PWL S3 access vs Rebirth 第二场 12.17
2020/12/18 DOTA
Python中用psycopg2模块操作PostgreSQL方法
2017/11/28 Python
Python Tkinter实现简易计算器功能
2018/01/30 Python
python+pyqt5编写md5生成器
2019/03/18 Python
Python math库 ln(x)运算的实现及原理
2019/07/17 Python
Apache部署Django项目图文详解
2019/07/30 Python
python requests抓取one推送文字和图片代码实例
2019/11/04 Python
html5 application cache遇到的严重问题
2012/12/26 HTML / CSS
LN-CC中国:高端男装和女装的奢侈时尚目的地
2019/09/14 全球购物
学校食堂采购员岗位职责
2013/12/05 职场文书
计算机专业毕业生自荐书
2014/06/02 职场文书
我的职业生涯规划:打造自己的运动帝国
2014/09/18 职场文书
党支部班子“四风”问题自我剖析材料
2014/09/28 职场文书
2014年医院党建工作总结
2014/12/20 职场文书
六一活动主持词
2015/06/30 职场文书
热爱劳动主题班会
2015/08/14 职场文书
《世界多美呀》教学反思
2016/02/22 职场文书
《哪吒之魔童降世》观后感:世上哪有随随便便的成功
2019/11/08 职场文书
mysql sock文件存储了什么信息
2022/07/15 MySQL