Let"s not dwell on the concept of anti-shaking and throttling, but let"s ask what"s the difference between the two. The first is the implementation of underscore
. _.now = function (){return new Date().getTime()};
_.debounce = function(func, wait, immediate){
var timeout, args, context, timestamp, result;
var later = function(){
var last = _.now() - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last); //
}else{
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
}
};
return function(){
context = this;
args = arguments;
timestamp = _.now();
if (!timeout)
timeout = setTimeout(later, wait);
return result;
};
};
split
_.now = function (){return new Date().getTime()};
_.debounce = function(func, wait, immediate){
var timeout, args, context, timestamp, result;
var later = function(){
timeout = null; //
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function(){
context = this;
args = arguments;
timestamp = _.now();
if (!timeout)
timeout = setTimeout(later, wait);
return result;
};
};
what"s the difference between the above two?