Created
September 12, 2015 07:14
-
-
Save roidayan/d80ccf6d6a9721081fe5 to your computer and use it in GitHub Desktop.
debounce service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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