From 99480dc8d36ac9ecb8a56b64ca1a47ff0be22894 Mon Sep 17 00:00:00 2001 From: aixianling Date: Wed, 22 Mar 2023 16:30:43 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=B2=E6=8A=96=E8=8A=82=E6=B5=81=E8=A3=85?= =?UTF-8?q?=E9=A5=B0=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/lib/js/decorator.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/ui/lib/js/decorator.js b/ui/lib/js/decorator.js index a086892f..0bcc2656 100644 --- a/ui/lib/js/decorator.js +++ b/ui/lib/js/decorator.js @@ -45,15 +45,14 @@ export function loading() { * @returns {(function(*, *, *): void)|*} */ export function throttle(wait) { - return function (target, name, descriptor) { + let timer; + return function (t, n, descriptor) { const origin = descriptor.value - let lock = false descriptor.value = function () { - if (!lock) { - lock = true - origin.apply(this, arguments) + if (!timer) { setTimeout(() => { - lock = false + origin.apply(this, arguments) + timer = null }, wait) } } @@ -83,3 +82,21 @@ export function load(sdk, interval = 200, name = "") { } } } + +/** + * 防抖装饰器 + * @param delay 防抖时间 + * @returns {(function(*, *, *): void)|*} + */ +export function debounce(delay) { + let timer = null; + return function (t, n, descriptor) { + const origin = descriptor.value + descriptor.value = function () { + clearTimeout(timer); + timer = setTimeout(() => { + origin.apply(this, arguments) + }, delay) + } + } +}