Browse Source

Added throttle() to Utils. Debounce() can now accept a scope as an optional argument.

dev-tmpl
Bryan Miller 5 years ago
parent
commit
173a43348e
1 changed files with 26 additions and 2 deletions
  1. +26
    -2
      app/js/common/Utils.js

+ 26
- 2
app/js/common/Utils.js View File

} }
}, },


debounce:function(func, delay){
debounce:function(func, delay, scope){
var timeout = null; var timeout = null;
return function(){ return function(){
var context = this;
//var context = this;
var context = scope || this;
var args = arguments; var args = arguments;
clearTimeout(timeout); clearTimeout(timeout);
timeout = setTimeout(function(){ timeout = setTimeout(function(){
func.apply(context, args); func.apply(context, args);
}, delay); }, delay);
}; };
},

throttle:function(func, threshold, scope){
threshold || (threshold = 250);
var lst = 0;
var timer;

return function(){
var context = scope || this;
var args = arguments;

var now = Date.now();
if (now < lst + threshold){
clearTimeout(timer);
timer = setTimeout(function(){
lst = now;
func.apply(context, args);
}, threshold);
} else {
lst = now;
func.apply(context, args);
}
};
} }
}; };



Loading…
Cancel
Save