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 相关文章推荐
三种检测iPhone/iPad设备方向的方法
Apr 23 Javascript
Js操作树节点自动折叠展开的几种方法
May 05 Javascript
js图片模糊切换显示特效的方法
Feb 17 Javascript
javascript引用类型指针的工作方式
Apr 13 Javascript
JS实现弹出浮动窗口(支持鼠标拖动和关闭)实例详解
Aug 06 Javascript
深入解析JavaScript的闭包机制
Oct 20 Javascript
深入浅析Bootstrap列表组组件
May 03 Javascript
详解vuejs几种不同组件(页面)间传值的方式
Jun 01 Javascript
jQuery实现html table行Tr的复制、删除、计算功能
Jul 10 jQuery
用Vue写一个分页器的示例代码
Apr 22 Javascript
WebStorm无法正确识别Vue3组合式API的解决方案
Feb 18 Vue.js
javascript实现用户必须勾选协议实例讲解
Mar 24 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重定向的三种方法分享
2012/02/22 PHP
php自定义中文字符串截取函数substr_for_gb2312及substr_for_utf8示例
2016/05/28 PHP
php中static和const关键字用法分析
2016/12/07 PHP
用PHP将Unicode 转化为UTF-8的实现方法(推荐)
2017/02/08 PHP
js触发asp.net的Button的Onclick事件应用
2013/02/02 Javascript
js/jquery解析json和数组格式的方法详解
2014/01/09 Javascript
jQuery实现菜单感应鼠标滑动动画效果的方法
2015/02/28 Javascript
javascript中的五种基本数据类型
2015/08/26 Javascript
JavaScript中的原型prototype完全解析
2016/05/10 Javascript
jquery.validate使用详解
2016/06/02 Javascript
JS实现刷新父页面不弹出提示框的方法
2016/06/22 Javascript
原生js仿jquery实现对Ajax的封装
2016/10/04 Javascript
vue上传图片组件编写代码
2017/07/26 Javascript
jQuery实现简单的下拉菜单导航功能示例
2017/12/07 jQuery
Python上传package到Pypi(代码简单)
2016/02/06 Python
Python异常处理操作实例详解
2018/05/10 Python
关于python列表增加元素的三种操作方法
2018/08/22 Python
python发送告警邮件脚本
2018/09/17 Python
Django中数据库的数据关系:一对一,一对多,多对多
2018/10/21 Python
django 类视图的使用方法详解
2019/07/24 Python
python GUI库图形界面开发之PyQt5工具栏控件QToolBar的详细使用方法与实例
2020/02/28 Python
jupyter notebook 恢复误删单元格或者历史代码的实现
2020/04/17 Python
用python实现名片管理系统
2020/06/18 Python
8款使用 CSS3 实现超炫的 Loading(加载)的动画效果
2015/03/17 HTML / CSS
中学教师实习自我鉴定
2013/09/28 职场文书
毕业生怎样写好自荐信
2013/11/11 职场文书
中学教师教育感言
2014/02/21 职场文书
经典洗发水广告词
2014/03/13 职场文书
法制报告会主持词
2014/04/02 职场文书
协议书模板
2014/04/23 职场文书
2014年母亲节演讲稿范文
2014/05/07 职场文书
体育比赛口号
2014/06/09 职场文书
房屋财产继承协议书范本
2014/11/03 职场文书
撤诉状格式范本
2015/05/19 职场文书
python微信智能AI机器人实现多种支付方式
2022/04/12 Python
win10壁纸在哪个文件夹 win10桌面背景图片文件位置分享
2022/08/05 数码科技