Throttle throttle function

/ * 
 * Throttle 
 * / 

var Throttle = function (the Fn, interval The) { 
  function var __self = fn // save a reference to the need of delays 
  var timer // timer 
  var firstTime = true // whether it is the first call 

  return function () { 
    var args = arguments 
    var = __me the this 

    IF (firstTime) {// If this is the first call, without delay execution 
      __self.apply (__ Me, args) 
      return to false firstTime = 

    } 

    IF (Timer) {// if a delay timer is also performed before, indicating that no complete 
      return to false 
    } 

    timer = the setTimeout (function () {// perform a delay 
      the clearTimeout (timer) 
      timer = null 
      __self.apply (__ Me, args) 
    }, interval The || 500)  
  }
}

window.onresize = throttle(function () {
  console.log(1)
}, 500)

  

Guess you like

Origin www.cnblogs.com/lan-cheng/p/12146677.html