From fa7e7cfb5b3ed2309605d9ee09e40252f158407e Mon Sep 17 00:00:00 2001 From: SillyLossy Date: Thu, 8 Jun 2023 16:29:45 +0300 Subject: [PATCH] Add throttle util --- public/scripts/utils.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/public/scripts/utils.js b/public/scripts/utils.js index 35d770d61..9e30a4fec 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -93,6 +93,17 @@ export function debounce(func, timeout = 300) { }; } +export function throttle(func, limit = 300) { + let lastCall; + return (...args) => { + const now = Date.now(); + if (!lastCall || (now - lastCall) >= limit) { + lastCall = now; + func.apply(this, args); + } + }; +} + export function isElementInViewport(el) { if (typeof jQuery === "function" && el instanceof jQuery) { el = el[0];