throttle functino in js code example
Example: js throttle function
function throttle (callback, limit) {
var wait = false; // Initially, we're not waiting
return function () { // We return a throttled function
if (!wait) { // If we're not waiting
callback.call(); // Execute users function
wait = true; // Prevent future invocations
setTimeout(function () { // After a period of time
wait = false; // And 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));