蝙蝠岛资源网 Design By www.hbtsch.com
本文实例讲述了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 "htmlcode">
// 秒表
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触发定时器,获取最新时间。时间差则为最新时间减去开始时间
PS:这里再为打击推荐一款功能相似的在线工具供大家参考:
在线秒表工具:
http://tools.jb51.net/bianmin/miaobiao
更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《AngularJS指令操作技巧总结》、《AngularJS入门与进阶教程》及《AngularJS MVC架构总结》
希望本文所述对大家AngularJS程序设计有所帮助。
标签:
Angular2,秒表
蝙蝠岛资源网 Design By www.hbtsch.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
蝙蝠岛资源网 Design By www.hbtsch.com
暂无Angular2实现的秒表及改良版示例的评论...
更新日志
2025年11月05日
2025年11月05日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]