This repository has been archived by the owner on May 24, 2023. It is now read-only.
/
angular-debounce.js
66 lines (66 loc) · 2.02 KB
/
angular-debounce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
angular.module('rt.debounce', []).factory('debounce', [
'$timeout',
function ($timeout) {
return function (wait, fn, no_postpone) {
var args, context, result, timeout;
var executed = true;
// Execute the callback function
function ping() {
result = fn.apply(context || this, args || []);
context = args = null;
executed = true;
}
// Cancel the timeout (for rescheduling afterwards).
function cancel() {
if (timeout) {
$timeout.cancel(timeout);
timeout = null;
}
}
// This is the actual result of the debounce call. It is a
// wrapper function which you can invoke multiple times and
// which will only be called once every "wait" milliseconds.
function wrapper() {
context = this;
args = arguments;
if (!no_postpone) {
cancel();
timeout = $timeout(ping, wait);
} else if (executed) {
executed = false;
timeout = $timeout(ping, wait);
}
}
// Forces the execution of pending calls
function flushPending() {
var pending = !!context;
if (pending) {
// Call pending, do it now.
cancel();
ping();
}
return pending;
}
// The wrapper also has a flush method, which you can use to
// force the execution of the last scheduled call to happen
// immediately (if any). It will also return the result of that
// call. Note that for asynchronous operations, you'll need to
// return a promise and wait for that one to resolve.
wrapper.flush = function () {
if (!flushPending() && !timeout) {
// Never been called.
ping();
}
return result;
};
// Flushes pending calls if any
wrapper.flushPending = function () {
flushPending();
return result;
};
// Cancels the queued execution if any
wrapper.cancel = cancel;
return wrapper;
};
}
]);