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