You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
606 B

  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;