diff --git a/README.md b/README.md index d142087..f082649 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/dist/angular-debounce.js b/dist/angular-debounce.js index 826274b..c013554 100644 --- a/dist/angular-debounce.js +++ b/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() { @@ -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); } } diff --git a/dist/angular-debounce.min.js b/dist/angular-debounce.min.js index a9ecedb..8a9e21f 100644 --- a/dist/angular-debounce.min.js +++ b/dist/angular-debounce.min.js @@ -1 +1 @@ -angular.module("rt.debounce",[]).factory("debounce",["$timeout",function(a){return function(b,c,d){function e(){k=c.apply(j||this,i||[]),j=i=null,n=!0}function f(){l&&(a.cancel(l),l=null)}function g(){j=this,i=arguments,m?n&&(n=!1,l=a(e,b)):(f(),l=a(e,b))}function h(){var a=!!j;return a&&(f(),e()),a}var i,j,k,l;d=d||{};var m=!!d.throttle,n=!0;return g.flush=function(){return h()||l||e(),k},g.flushPending=function(){return h(),k},g.cancel=f,g}}]); \ No newline at end of file +angular.module("rt.debounce",[]).factory("debounce",["$timeout",function(a){return function(b,c,d){function e(){k=c.apply(j||this,i||[]),j=i=null,m=!0}function f(){l&&(a.cancel(l),l=null)}function g(){j=this,i=arguments,d?m&&(m=!1,l=a(e,b)):(f(),l=a(e,b))}function h(){var a=!!j;return a&&(f(),e()),a}var i,j,k,l,m=!0;return g.flush=function(){return h()||l||e(),k},g.flushPending=function(){return h(),k},g.cancel=f,g}}]); \ No newline at end of file diff --git a/src/debounce.js b/src/debounce.js index a9f8c95..1080840 100644 --- a/src/debounce.js +++ b/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). @@ -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); } }