Skip to content

Instantly share code, notes, and snippets.

@roidayan
Created September 12, 2015 07:14
Show Gist options
  • Select an option

  • Save roidayan/d80ccf6d6a9721081fe5 to your computer and use it in GitHub Desktop.

Select an option

Save roidayan/d80ccf6d6a9721081fe5 to your computer and use it in GitHub Desktop.
debounce service
angular
.module('app')
.factory('debounce', ['$timeout', DebounceService]);
function DebounceService($timeout) {
return function(func, wait) {
var timeout, args, context, result;
function debounce() {
context = this;
args = arguments;
var later = function () {
timeout = null;
result = func.apply(context, args);
};
if (timeout) {
$timeout.cancel(timeout);
}
timeout = $timeout(later, wait);
}
debounce.cancel = function () {
if (timeout) {
$timeout.cancel(timeout);
timeout = null;
}
};
return debounce;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment