throttling in javascript code example

Example 1: 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));

Example 2: 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));