Skip to content

Instantly share code, notes, and snippets.

@matteodelabre
Last active August 29, 2015 14:00
Show Gist options
  • Save matteodelabre/11098809 to your computer and use it in GitHub Desktop.
Save matteodelabre/11098809 to your computer and use it in GitHub Desktop.
AMD module that enables to debounce functions, i.e to limit them to be called once every X seconds
/*globals define */
define(function () {
'use strict';
/**
* debounce module
* This module is freely usable, without any license
* Based on John Resig's work
*
* @param {function} [func] - Function to be debounced
* @param {int} [treshold=100] - Time limit
**/
return function (func, threshold) {
var timeout;
return function debounced() {
var obj = this, args = arguments;
function delayed() {
func.apply(obj, args);
timeout = null;
}
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(delayed, threshold || 100);
};
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment