Throttling function (to prevent the repeat request) applet

In the first statement util.js and export functions

 

function throttle(fn, gapTime) {
    if (gapTime == null || gapTime == undefined) {
        gapTime = 1500
    }

    let _lastTime = null

    // returns the new function 
    return  function () {
        let _nowTime = + new Date()
        if (_nowTime - _lastTime > gapTime || !_lastTime) {
            fn.apply ( this , arguments)    // The parameters passed to the original function and this 
            _lastTime = _nowTime
        }
    }
}
module.exports ={ throttle: throttle }

Js required page in the document introduction var util = require ( '')

tap: util.throttle(function (e) {
 // function on their own needs
}, 1000) // interval 1000

  

Guess you like

Origin www.cnblogs.com/Alitar/p/11975001.html