Micro letter applet throttle function (preventing multiple clicks, can be used to "pay immediately, the page jumps, etc.")

Function throttle (throttle): function over a period of time to perform multiple triggers only the first time before the end of this period of time, no matter how many times it will not trigger the execution of the function.

 1. Add utils.js file

Throttle function (Fn, gapTime) { 
    IF (gapTime == null || gapTime == undefined) { 
        gapTime = 1500 
    } 
 
    the let _lastTime = null 
 
    // returns the new function 
    return function () { 
        the let + = new new _nowTime a Date () 
        IF (! _nowTime - _lastTime> gapTime || _lastTime) { 
            fn.apply (this, arguments) // and this parameter to the primitive 
            _lastTime = _nowTime 
        } 
    } 
} 
 
module.exports = { 
  Throttle: Throttle 
}

 2. Introduction utils.js the page you want to use

util = const require ( '../../ utils / util.js')

 use

 handle: util.throttle(function () {
     
    })

  

Guess you like

Origin www.cnblogs.com/Glant/p/11423359.html