From 8d58cb43b3002b63daf057da383832dd56333af9 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:16:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=98=B2=E6=8A=96=E5=87=BD?= =?UTF-8?q?=E6=95=B0Debounce?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JS/Debounce.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 JS/Debounce.js diff --git a/JS/Debounce.js b/JS/Debounce.js new file mode 100644 index 0000000..bd014c8 --- /dev/null +++ b/JS/Debounce.js @@ -0,0 +1,25 @@ + +/** + * 防抖方法。实例化一个Debounce对象,然后调用run方法执行防抖函数。 + */ +class Debounce { + constructor(time) { + //执行间隔 + this.timeout = time; + //计时器 + this.timer = null; + } + //等待时间结束后执行方法 + run(func, ...args) { + if (this.timer) { + clearTimeout(this.timer); + this.timer = null; + } + if (this.timer == null) { + this.timer = setTimeout(() => { + func(...args); + this.timer = null; + }, this.timeout); + } + } +} \ No newline at end of file