瀏覽代碼

Class mesure temps d'execution

Arthur Brandao 5 年之前
父節點
當前提交
2c445dc178
共有 1 個文件被更改,包括 41 次插入0 次删除
  1. 41 0
      src/timer.js

+ 41 - 0
src/timer.js

@@ -0,0 +1,41 @@
+const timer = class Timer {
+
+    constructor() {
+        this.begin = 0;
+        this.end = 0;
+    }
+
+    start() {
+        this.begin = this._timestamp();
+    }
+
+    stop() {
+        this.end = this._timestamp();
+    }
+
+    second() {
+        if (this.begin === 0 || this.end === 0) {
+            return 0;
+        }
+        return this.end - this.begin;
+    }
+
+    time() {
+        const time = this.second();
+        let result;
+        if ((time / 60) >= 1) {
+            const tmp = parseInt(time / 60);
+            result = tmp + 'm ' + (time - (tmp * 60)) + 's';
+        } else {
+            result = time + 's';
+        }
+        return result;
+    }
+
+    _timestamp() {
+        return parseInt(Date.now() / 1000);
+    }
+
+};
+
+module.exports = new timer();