index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict'
  2. function ReInterval (callback, interval, args) {
  3. var self = this;
  4. this._callback = callback;
  5. this._args = args;
  6. this._interval = setInterval(callback, interval, this._args);
  7. this.reschedule = function (interval) {
  8. // if no interval entered, use the interval passed in on creation
  9. if (!interval)
  10. interval = self._interval;
  11. if (self._interval)
  12. clearInterval(self._interval);
  13. self._interval = setInterval(self._callback, interval, self._args);
  14. };
  15. this.clear = function () {
  16. if (self._interval) {
  17. clearInterval(self._interval);
  18. self._interval = undefined;
  19. }
  20. };
  21. this.destroy = function () {
  22. if (self._interval) {
  23. clearInterval(self._interval);
  24. }
  25. self._callback = undefined;
  26. self._interval = undefined;
  27. self._args = undefined;
  28. };
  29. }
  30. function reInterval () {
  31. if (typeof arguments[0] !== 'function')
  32. throw new Error('callback needed');
  33. if (typeof arguments[1] !== 'number')
  34. throw new Error('interval needed');
  35. var args;
  36. if (arguments.length > 0) {
  37. args = new Array(arguments.length - 2);
  38. for (var i = 0; i < args.length; i++) {
  39. args[i] = arguments[i + 2];
  40. }
  41. }
  42. return new ReInterval(arguments[0], arguments[1], args);
  43. }
  44. module.exports = reInterval;