function throttle(fn, ms) {
let isThrottled = false;
return function(...args) {
if (isThrottled) return;
fn.apply(this, args);
isThrottled = true;
setTimeout(() => {
isThrottled = false;
}, ms);
};
}
const t = throttle(x => console.log(x), 400);
t('A'); // 0мс
setTimeout(() => t('B'), 300); // 300мс
setTimeout(() => t('C'), 450); // 450мс
setTimeout(() => t('D'), 700); // 700мс
setTimeout(() => t('E'), 900); // 900мс
Code will run automatically after changes.
Code runs in an isolated environment.
Code runs in an isolated environment.