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 相关文章推荐
热点新闻滚动特效的js代码
Aug 17 Javascript
js清除input中type等于file的值域(示例代码)
Dec 24 Javascript
JavaScript实现从数组中选出和等于固定值的n个数
Sep 03 Javascript
jQuery模拟原生态App上拉刷新下拉加载更多页面及原理
Aug 10 Javascript
简单纯js实现点击切换TAB标签实例
Aug 23 Javascript
JavaScript动态数量的文件上传控件
Nov 18 Javascript
React Native中TabBarIOS的简单使用方法示例
Oct 13 Javascript
通过一个简单的例子学会vuex与模块化
Nov 22 Javascript
微信小程序 setData 对 data数据影响问题
Apr 18 Javascript
vue-router两种模式区别及使用注意事项详解
Aug 01 Javascript
Vue项目接入Paypal实现示例详解
Jun 04 Javascript
vue-cli3 引入 font-awesome的操作
Aug 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代码(星期六,星期日总和)
2009/11/12 PHP
javascript prototype 原型链
2009/03/12 Javascript
dwr spring的集成实现代码
2009/03/22 Javascript
jQuery 性能优化指南(2)
2009/05/21 Javascript
IE6中使用position导致页面变形的解决方案(js代码)
2011/01/09 Javascript
关于IE BUG与字符串截取substr的解决办法
2013/04/10 Javascript
javascript写的一个模拟阅读小说的程序
2014/04/04 Javascript
什么是MEAN?JavaScript编程中的MEAN是什么意思?
2014/12/18 Javascript
使用JavaScript实现旋转的彩圈特效
2015/06/23 Javascript
jQuery中的基本选择器用法学习教程
2016/04/14 Javascript
使用jquery获取url及url参数的简单实例
2016/06/14 Javascript
浅谈js中的in-for循环
2016/06/28 Javascript
AngularJS实现ajax请求的方法
2016/11/22 Javascript
JS实现“隐藏与显示”功能(多种方法)
2016/11/24 Javascript
在Vue中使用highCharts绘制3d饼图的方法
2018/02/08 Javascript
vue中添加与删除关键字搜索功能
2019/10/12 Javascript
react-native聊天室|RN版聊天App仿微信实例|RN仿微信界面
2019/11/12 Javascript
JavaScript闭包原理与用法学习笔记
2020/05/29 Javascript
jQuery 移除事件的方法
2020/06/20 jQuery
微信小程序实现点击导航标签滚动定位到对应位置
2020/11/19 Javascript
[03:01]完美世界DOTA2联赛PWL S2 集锦第二期
2020/12/03 DOTA
[49:11]完美世界DOTA2联赛PWL S3 INK ICE vs DLG 第二场 12.20
2020/12/23 DOTA
python简单获取本机计算机名和IP地址的方法
2015/06/03 Python
numpy使用技巧之数组过滤实例代码
2018/02/03 Python
Pycharm设置去除显示的波浪线方法
2018/10/28 Python
分享Python切分字符串的一个不错方法
2018/12/14 Python
Python实战购物车项目的实现参考
2019/02/20 Python
python网络爬虫实现发送短信验证码的方法
2021/02/25 Python
迪奥官网:Dior.com
2018/12/04 全球购物
Noon埃及:埃及在线购物
2019/11/26 全球购物
英国豪华装饰照明品牌的在线零售商:Inspyer Lighting
2019/12/10 全球购物
How TDD works
2012/09/30 面试题
新春文艺演出主持词
2014/03/27 职场文书
四川省传达学习贯彻党的群众路线教育实践活动总结大会精神新闻稿
2014/10/26 职场文书
校车安全管理责任书
2015/05/11 职场文书
PHP中->和=>的意思
2021/03/31 PHP