Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions func/throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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)
}
}
}
Expand Down