Angular2实现的秒表及改良版示例-创新互联
本文实例讲述了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);
}
}
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文标题:Angular2实现的秒表及改良版示例-创新互联
转载注明:http://www.jxjierui.cn/article/dieiph.html


咨询
建站咨询
