Skip to content

Instantly share code, notes, and snippets.

@wangpin34
Last active September 9, 2019 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangpin34/fdc90d9183d280c9d0bd715eb4a07404 to your computer and use it in GitHub Desktop.
Save wangpin34/fdc90d9183d280c9d0bd715eb4a07404 to your computer and use it in GitHub Desktop.
light lodash
const debounce = (func, delay) => {
let inDebounce
return function() {
const context = this
const args = arguments
clearTimeout(inDebounce)
inDebounce = setTimeout(() => func.apply(context, args), delay)
}
}
const throttle = (func, limit) => {
let lastFunc
let lastRan
return function() {
const context = this
const args = arguments
if (!lastRan) {
func.apply(context, args)
lastRan = Date.now()
} else {
clearTimeout(lastFunc)
lastFunc = setTimeout(function() {
if (Date.now() - lastRan >= limit) {
func.apply(context, args)
lastRan = Date.now()
}
}, limit - (Date.now() - lastRan))
}
}
}
const isNumber = arg => {
return typeof arg === 'number' && Math.floor(arg) === arg
}
const isFunction = arg => {
return typeof arg === 'function'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment