From 0809fce7c43a94c40f0b355e359221477facc51b Mon Sep 17 00:00:00 2001 From: chensiAb <chenchenco03@163.com> Date: Thu, 16 May 2024 11:01:32 +0800 Subject: [PATCH] fix:'格式化大写PX变小写' --- common/Tools.js | 41 +++++++++++++++++++---------------------- 1 files changed, 19 insertions(+), 22 deletions(-) diff --git a/common/Tools.js b/common/Tools.js index 624297d..72d2079 100644 --- a/common/Tools.js +++ b/common/Tools.js @@ -542,42 +542,40 @@ * @param {Function} fnc * @param {Number} wait * @returns {Function} + * @tutorial Tools.debounce(<fnc>, <wait>)(<DebounceKey>) */ static debounce(fnc, wait = 200) { - let timeout = null; - return function() { - const context = this; - const args = arguments; - if (timeout) { - clearTimeout(timeout); + return key => { + if (!key || !Tools.isString(key)) { + throw 'Debounce function need a key!'; } - const callNow = !timeout; - timeout = setTimeout(() => { - timeout = null; - }, wait); - if (callNow) { - fnc.apply(context, args); + const timer = Tools.debounce['dKey-' + key]; + if (timer) { + clearTimeout(timer); } + Tools.debounce['dKey-' + key] = setTimeout(fnc, wait); }; } /** - * 节流函数(一段时间周期内,仅执行第一次回调) + * 节流函数(本段时间内,仅执行第一次回调) * 在一定时间内只能触发一次函数执行 * 如果这个时间内再次触发,则忽略,直到下一个时间段再次触发 * @param {Function} fnc * @param {Number} wait - * @returns {Function} + * @returns {function(key):void} + * @tutorial Tools.throttle(<fnc>, <wait>)(<ThrottleKey>) */ static throttle(fnc, wait = 200) { - let previous = 0; - return function() { - const context = this; - const args = arguments; - const now = new Date(); + return key => { + if (!key || !Tools.isString(key)) { + throw 'Throttle function need a key!'; + } + const previous = Tools.throttle['tKey-' + key] || 0; + const now = Date.now(); if (now - previous > wait) { - fnc.apply(context, args); - previous = now; + Tools.throttle['tKey-' + key] = now; + fnc(); } }; } @@ -600,4 +598,3 @@ }; } -global.Tools = Tools; -- Gitblit v1.9.1