Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Small cleanups, docs
  • Loading branch information
rubenv committed May 17, 2016
1 parent b7c02a6 commit 6221a36
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -57,6 +57,14 @@ angular.module('myApp').controller('testCtrl', function (debounce) {
});
```

Repeatedly calling `fn()` will postpone indefinitely. Pass a third `true` parameter to also fire intermediate calls:

```js
var fn = debounce(2000, function () {
// Do things here.
}, true);
```

## License

(The MIT License)
Expand Down
14 changes: 6 additions & 8 deletions dist/angular-debounce.js
@@ -1,16 +1,14 @@
angular.module('rt.debounce', []).factory('debounce', [
'$timeout',
function ($timeout) {
return function (wait, fn, opts) {
return function (wait, fn, no_postpone) {
var args, context, result, timeout;
opts = opts || {};
var throttling = !!opts.throttle;
var throttled = true;
var executed = true;
// Execute the callback function
function ping() {
result = fn.apply(context || this, args || []);
context = args = null;
throttled = true;
executed = true;
}
// Cancel the timeout (for rescheduling afterwards).
function cancel() {
Expand All @@ -25,11 +23,11 @@ angular.module('rt.debounce', []).factory('debounce', [
function wrapper() {
context = this;
args = arguments;
if (!throttling) {
if (!no_postpone) {
cancel();
timeout = $timeout(ping, wait);
} else if (throttled) {
throttled = false;
} else if (executed) {
executed = false;
timeout = $timeout(ping, wait);
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-debounce.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions src/debounce.js
@@ -1,16 +1,13 @@
angular.module('rt.debounce', []).factory('debounce', function ($timeout) {

return function (wait, fn, opts) {
return function (wait, fn, no_postpone) {
var args, context, result, timeout;
opts = opts || {};
var throttling = !!opts.throttle;
var throttled = true;
var executed = true;

// Execute the callback function
function ping() {
result = fn.apply(context || this, args || []);
context = args = null;
throttled = true;
executed = true;
}

// Cancel the timeout (for rescheduling afterwards).
Expand All @@ -27,11 +24,11 @@ angular.module('rt.debounce', []).factory('debounce', function ($timeout) {
function wrapper() {
context = this;
args = arguments;
if (!throttling) {
if (!no_postpone) {
cancel();
timeout = $timeout(ping, wait);
} else if (throttled) {
throttled = false;
} else if (executed) {
executed = false;
timeout = $timeout(ping, wait);
}
}
Expand Down

0 comments on commit 6221a36

Please sign in to comment.