From 47bcbba772b4db1528491f38b9a1e4951d2c966a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E9=95=BF=E5=BE=81?= Date: Mon, 21 Aug 2023 15:18:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=98=B2=E6=8A=96=E6=96=B9?= =?UTF-8?q?=E6=B3=95DebounceImmediately?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JS/DebounceImmediately.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 JS/DebounceImmediately.js 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