diff --git a/JS/DebounceImmediately.js b/JS/DebounceImmediately.js new file mode 100644 index 0000000..45d7534 --- /dev/null +++ b/JS/DebounceImmediately.js @@ -0,0 +1,20 @@ +/** + * DebounceImmediately跟Debounce的区别是会首先立即执行一次。 + */ +class DebounceImmediately { + constructor(time) { + //执行间隔 + this.timeout = time; + //计时器 + this.timer = null; + } + run(func, ...args) { + if (this.timer) { + return; + } + this.timer = setTimeout(() => { + this.timer = null; + }, this.timeout); + func(...args); + } +} \ No newline at end of file