Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions JavaScript/c-throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

// Function throttling, executed once per interval

const throttle = (timeout, f, ...args) => {
const throttle = (delay, f, ...args) => {
let timer;
let wait = false;
let wrapped = null;
let pendingArgs = null;

const throttled = (...par) => {
const throttled = () => {
timer = undefined;
if (wait) wrapped(...par);
if (pendingArgs) {
wrapped(...pendingArgs);
pendingArgs = null;
}
};

wrapped = (...par) => {
const wrapped = (...params) => {
if (!timer) {
timer = setTimeout(throttled, timeout, ...par);
wait = false;
return f(...args.concat(par));
timer = setTimeout(throttled, delay);
return f(...args, ...params);
}
wait = true;
pendingArgs = params;
return null;
};

Expand All @@ -31,11 +32,13 @@ const fn = (...args) => {
console.log('Function called, args:', args);
};

const ft = throttle(200, fn, 'value1');
const ft = throttle(200, fn, 'throttled');

let calls = 0;
const timer = setInterval(() => {
fn('value2');
ft('value3');
calls++;
fn(calls);
ft(calls);
}, 50);

setTimeout(() => {
Expand Down