diff --git a/src/util/misc.js b/src/util/misc.js index 39661eb448..bcde9b9412 100644 --- a/src/util/misc.js +++ b/src/util/misc.js @@ -29,10 +29,28 @@ export function countColumn(string, end, tabSize, startIndex, startValue) { } export class Delayed { - constructor() {this.id = null} + constructor() { + this.id = null + this.f = null + this.time = 0 + this.handler = bind(this.onTimeout, this) + } + onTimeout(self) { + self.id = 0 + if (self.time < +new Date) { + self.f() + } else { + setTimeout(self.handler, self.time - +new Date) + } + } set(ms, f) { - clearTimeout(this.id) - this.id = setTimeout(f, ms) + this.f = f + const time = +new Date + ms + if (!this.id || time < this.time) { + clearTimeout(this.id) + this.id = setTimeout(this.handler, ms) + this.time = time + } } }