ZhenPort: Fixed a bug

This commit is contained in:
BOTAlex 2024-03-31 03:50:29 +02:00
parent 8ed6d26c40
commit e75b6358ef
3 changed files with 132 additions and 105 deletions

View file

@ -0,0 +1,21 @@
export function throttle(callback, wait) {
let timeoutId = null;
let lastExecutedTime = 0;
return function (...args) {
const currentTime = Date.now();
const execute = () => {
lastExecutedTime = currentTime;
callback.apply(this, args);
};
if (currentTime - lastExecutedTime >= wait) {
execute();
} else {
clearTimeout(timeoutId);
timeoutId = setTimeout(execute, wait - (currentTime - lastExecutedTime));
}
};
}