Last active
May 16, 2018 22:44
-
-
Save airton/05befd02380442a18a578f8f3945cf44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function throttle( func, limit ){ | |
var inThrottle; | |
var lastFunc; | |
var lastRan; | |
return function() { | |
var context = this; | |
var args = arguments; | |
if (!inThrottle) { | |
func.apply(context, args); | |
lastRan = Date.now(); | |
inThrottle = true; | |
} else { | |
clearTimeout(lastFunc); | |
lastFunc = setTimeout(function() { | |
if ((Date.now() - lastRan) >= limit) { | |
func.apply(context, args); | |
lastRan = Date.now(); | |
} | |
}, limit - (Date.now() - lastRan)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment