uploadtimer.js 606 B

1234567891011121314151617181920212223242526
  1. class UploadTimer {
  2. /**
  3. * @constructor
  4. * @param {number} timeout - timer timeout in msecs.
  5. * @param {Function} callback - callback to run when timeout reached.
  6. */
  7. constructor(timeout = 0, callback = () => {}) {
  8. this.timeout = timeout;
  9. this.callback = callback;
  10. this.timer = null;
  11. }
  12. clear() {
  13. clearTimeout(this.timer);
  14. }
  15. set() {
  16. // Do not start a timer if zero timeout or it hasn't been set.
  17. if (!this.timeout) return false;
  18. this.clear();
  19. this.timer = setTimeout(this.callback, this.timeout);
  20. return true;
  21. }
  22. }
  23. module.exports = UploadTimer;