Write a function named throttle
that receives one parameter:
callback
The throttle
function should return a new function, let's name it throttledCallback
that will behave exactly like callback
but it will only allow us to call it again if less than 1000ms has passed since the last invocation.
This means that additional calls will be ignored.
const printSum = (a, b) => {
console.log(a + b);
}
const throttledPrintSum = throttle(printSum);
throttledPrintSum(1, 2);
throttledPrintSum(11, 12);
throttledPrintSum(100, 200);
/**
* Only the number 3 should be printed to the console.
* That's because the function is throttled so it only runs
* once every 1000ms. The first time we call it, the function
* runs. Afterwards, the 2nd and 3rd calls are ignored, because
* they happen sooner than 1000ms after the first call.
*/