From dee5402aa5f9ae05409c49e0a54e3f04f8ca31da Mon Sep 17 00:00:00 2001 From: Ahmed Saber Date: Sat, 15 Jul 2023 03:04:41 +0300 Subject: [PATCH] Refactor throttle in utils/fn.js --- src/js/utils/fn.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/js/utils/fn.js b/src/js/utils/fn.js index cf7ba35ed..df5ccc476 100644 --- a/src/js/utils/fn.js +++ b/src/js/utils/fn.js @@ -60,19 +60,15 @@ export const bind_ = function(context, fn, uid) { * * @return {Function} */ -export const throttle = function(fn, wait) { - let last = window.performance.now(); - - const throttled = function(...args) { - const now = window.performance.now(); - - if (now - last >= wait) { - fn(...args); - last = now; - } +export const throttle = function(func, delay) { + let lastCallTime = 0; + return function throttled(...args) { + const currentTime = Date.now() + if (currentTime - lastCallTime >= delay) { + lastCallTime = currentTime; + func(...args); + } }; - - return throttled; }; /**