what is the difference between debounce and throttle in javascript code example
Example: throttoling javascript
function throttle (callback, limit) {
var wait = false; // initial step when we are nit waiting
return function () { // returnimg a throttled function
if (!wait) { // while not waiting
callback.call(); // Executing users function
wait = true; // Preventing future invocations
setTimeout(function () { // after certain interval of time
wait = false; // allow future invocations
}, limit);
}
}
}
// Usage Example:
// On scroll, allow function to run at most 1 time per 100ms
window.addEventListener("scroll", throttle(function(){
/*stuff to be throttled*/
}, 100));