ascor
Version:
一些常用的简单的js工具
37 lines (36 loc) • 758 B
JavaScript
import { getCurrentTime } from "./getCurrentTime";
/**
* 计时器,例:new Timer()
*/
export const Timer = class {
constructor() {
this.isStart = false;
this.begin = getCurrentTime(true);
}
/**
* 开始计时
*/
start() {
if (!this.isStart) {
this.isStart = true;
}
this.begin = getCurrentTime(true);
}
/**
* 步进,执行一次返回经过的时间毫秒数
*/
step() {
if (!this.isStart) {
return 0;
}
return getCurrentTime(true) - this.begin;
}
/**
* 停止计时,返回经过的毫秒数
*/
stop() {
let s = this.step();
this.isStart = false;
return s;
}
};