From ebc512793cbe78c493882bf3a98209e49dc67d9a Mon Sep 17 00:00:00 2001 From: tenorok Date: Sat, 9 May 2020 17:16:41 +0300 Subject: [PATCH] debounce: invokeAsap param should`t prevent trailing calling. --- src/jquery.debounce/jquery.debounce.js | 16 ++++++++++++---- src/jquery.debounce/jquery.debounce.min.js | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/jquery.debounce/jquery.debounce.js b/src/jquery.debounce/jquery.debounce.js index 41ae3ad..6451ffb 100644 --- a/src/jquery.debounce/jquery.debounce.js +++ b/src/jquery.debounce/jquery.debounce.js @@ -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); @@ -79,4 +87,4 @@ $.extend({ }); -})(jQuery); \ No newline at end of file +})(jQuery); diff --git a/src/jquery.debounce/jquery.debounce.min.js b/src/jquery.debounce/jquery.debounce.min.js index f2cba76..436f5b7 100644 --- a/src/jquery.debounce/jquery.debounce.min.js +++ b/src/jquery.debounce/jquery.debounce.min.js @@ -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); \ No newline at end of file + */(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);