Last active
November 3, 2024 14:13
-
-
Save maoosi/f5efdf705a07d57553db to your computer and use it in GitHub Desktop.
Simple JavaScript throttle function - From Grafikart https://www.grafikart.fr/tutoriels/javascript/debounce-throttle-642
This file contains hidden or 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(callback, delay) { | |
var last; | |
var timer; | |
return function () { | |
var context = this; | |
var now = +new Date(); | |
var args = arguments; | |
if (last && now < last + delay) { | |
clearTimeout(timer); | |
timer = setTimeout(function () { | |
last = now; | |
callback.apply(context, args); | |
}, delay); | |
} else { | |
last = now; | |
callback.apply(context, args); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment