Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/jquery.debounce/jquery.debounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,26 @@ $.extend({
}

var timer;
var lastInvokeTime = 0;

function invoke(time, args) {
if (time - lastInvokeTime > timeout) {
fn.apply(this, args);
lastInvokeTime = time;
}
}

return function() {

var args = arguments;
var time = new Date().valueOf();
ctx = ctx || this;

invokeAsap && !timer && fn.apply(ctx, args);
invokeAsap && !timer && invoke.call(ctx, time, arguments);

clearTimeout(timer);

timer = setTimeout(function() {
invokeAsap || fn.apply(ctx, args);
invoke.call(ctx, time, arguments);
timer = null;
}, timeout);

Expand Down Expand Up @@ -79,4 +87,4 @@ $.extend({

});

})(jQuery);
})(jQuery);
2 changes: 1 addition & 1 deletion src/jquery.debounce/jquery.debounce.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/(function(a){a.extend({debounce:function(a,b,c,d){arguments.length==3&&typeof c!="boolean"&&(d=c,c=!1);var e;return function(){var f=arguments;d=d||this,c&&!e&&a.apply(d,f),clearTimeout(e),e=setTimeout(function(){c||a.apply(d,f),e=null},b)}},throttle:function(a,b,c){var d,e,f;return function(){e=arguments,f=!0,c=c||this,d||function(){f?(a.apply(c,e),f=!1,d=setTimeout(arguments.callee,b)):d=null}()}}})})(jQuery);
*/(function(e){e.extend({debounce:function(n,l,t,u){if(arguments.length==3&&typeof t!="boolean"){u=t;t=false}var i;var f=0;function o(e,t){if(e-f>l){n.apply(this,t);f=e}}return function(){var e=(new Date).valueOf();u=u||this;if(t&&!i){o.call(u,e,arguments)}clearTimeout(i);i=setTimeout(function(){o.call(u,e,arguments);i=null},l)}},throttle:function(e,t,n){var l,u,i;return function(){u=arguments;i=true;n=n||this;l||function(){if(i){e.apply(n,u);i=false;l=setTimeout(arguments.callee,t)}else{l=null}}()}}})})(jQuery);