diff --git a/func/throttle.js b/func/throttle.js index 5a8cf73..c829ccb 100644 --- a/func/throttle.js +++ b/func/throttle.js @@ -7,21 +7,20 @@ * @return {Function} */ function throttle (callback, wait, options) { - var args, context var opts = options || {} var runFlag = false var timeout = 0 var optLeading = 'leading' in opts ? opts.leading : true var optTrailing = 'trailing' in opts ? opts.trailing : false - var runFn = function () { + var runFn = function (args, context) { runFlag = true callback.apply(context, args) timeout = setTimeout(endFn, wait) } - var endFn = function () { + var endFn = function (args, context) { timeout = 0 if (!runFlag && optTrailing === true) { - runFn() + runFn(args, context) } } var cancelFn = function () { @@ -32,14 +31,14 @@ function throttle (callback, wait, options) { return rest } var throttled = function () { - args = arguments - context = this + const args = arguments + const context = this runFlag = false if (timeout === 0) { if (optLeading === true) { - runFn() + runFn(args, context) } else if (optTrailing === true) { - timeout = setTimeout(endFn, wait) + timeout = setTimeout(() => endFn(args, context), wait) } } }