What is the difference between these two implementations in anti-shaking?

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?


I suggest you figure out what throttling is and what is anti-shaking
and this is not recursive

. < hr >

the front one is anti-shaking, and the back one is throttling


I also admire that I can make throttling and anti-shaking so complicated.

function debounce(fn,delay){
    var timer = null; 
    return function(){
      clearTimeout(timer);
      timer = setTimeout(function(){
        fn.apply(this);
      },delay)
    }
}
Menu