Browse Source

- added getLast to check when last interval started
- updated stop and start for case when interval doesn't exist

Ksawery Kuklinski 8 years ago
parent
commit
c884f98938
1 changed files with 21 additions and 7 deletions
  1. 21 7
      intervalMenager.js

+ 21 - 7
intervalMenager.js

@@ -6,6 +6,7 @@ module.exports = class intervalMenager {
     };
     this.interval = 1000;
     this.id = 0;
+    this.lastInterval = null;
     return this;
   }
   setFunction(fn){
@@ -22,15 +23,28 @@ module.exports = class intervalMenager {
     this.interval = int;
     return this;
   }
+  getLast(){
+    return new Date() - this.lastInterval();
+  }
   start(){
-    var self = this;
-    this.intervalId = setInterval(function () {
-      self.function(++self.id);
-    }, this.interval);
-    return this;
+    if (this.intervalId === null) {
+      var self = this;
+      this.intervalId = setInterval(function () {
+        self.function(++self.id);
+        self.lastInterval = new Date();
+      }, this.interval);
+      return this;
+    }else{
+      throw new Error("Interval already started");
+    }
   }
   stop(){
-    clearInterval(this.intervalId);
-    return this;
+    if(this.intervalId !== null){
+      clearInterval(this.intervalId);
+      this.intervalId = null;
+      return this;
+    }else{
+      throw new Error("No interval to stop");
+    }
   }
 };