intervalMenager.js 783 B

123456789101112131415161718192021222324252627282930313233343536
  1. module.exports = class intervalMenager {
  2. constructor() {
  3. this.intervalId = null;
  4. this.function = function () {
  5. console.log("Somethink should be here");
  6. };
  7. this.interval = 1000;
  8. this.id = 0;
  9. return this;
  10. }
  11. setFunction(fn){
  12. if(typeof fn !== "function"){
  13. throw new TypeError("fn should be a function");
  14. }
  15. this.function = fn;
  16. return this;
  17. }
  18. setInterval(int){
  19. if (typeof int !== "number") {
  20. throw new TypeError("int should be a number");
  21. }
  22. this.interval = int;
  23. return this;
  24. }
  25. start(){
  26. var self = this;
  27. this.intervalId = setInterval(function () {
  28. self.function(++self.id);
  29. }, this.interval);
  30. return this;
  31. }
  32. stop(){
  33. clearInterval(this.intervalId);
  34. return this;
  35. }
  36. };