Skip to content

Instantly share code, notes, and snippets.

@gsmartagence
Created July 5, 2019 12:45
Show Gist options
  • Select an option

  • Save gsmartagence/8f368e32cc0896d6cde38cb1ed389285 to your computer and use it in GitHub Desktop.

Select an option

Save gsmartagence/8f368e32cc0896d6cde38cb1ed389285 to your computer and use it in GitHub Desktop.
{
"name": "throttle.js",
"version": "0.1.0"
}
/**
* Vanilla Throttle implementation
* https://gist.github.com/peduarte/969217eac456538789e8fac8f45143b4
* @param {*} func
* @param {*} wait
*/
const throttle = function(callback, wait, immediate = false) {
let timeout = null
let initialCall = true
return function() {
const callNow = immediate && initialCall
const next = () => {
callback.apply(this, arguments)
timeout = null
}
if (callNow) {
initialCall = false
next()
}
if (!timeout) {
timeout = setTimeout(next, wait)
}
}
}
export {throttle};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment