Skip to content

Instantly share code, notes, and snippets.

@travist
Last active October 2, 2021 11:28
Show Gist options
  • Save travist/057e613adbcd31510d6e110786d88e10 to your computer and use it in GitHub Desktop.
Save travist/057e613adbcd31510d6e110786d88e10 to your computer and use it in GitHub Desktop.
How to debounce a $scope.$watch for Angular.js.
['$scope', function($scope) {
var debounce = function(cb) {
var timeout = null;
return function(data) {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(function() {
cb(data);
}, 200);
};
};
$scope.$watch('submission.data', debounce(function(data) {
// Put your logic here....
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment