Skip to content

Instantly share code, notes, and snippets.

@camille-hdl
Last active September 3, 2020 09:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camille-hdl/fe651f2fadcc11444549 to your computer and use it in GitHub Desktop.
Save camille-hdl/fe651f2fadcc11444549 to your computer and use it in GitHub Desktop.
Function throttling implementation in ES6
/*
http://www.es6fiddle.net/i8d8e1um/
*/
var throttle = (func,ms=50,context=window) => {
let to;
let wait=false;
return (...args) => {
let later = () => {
func.apply(context,args);
};
if(!wait) {
later();
wait = true;
to = setTimeout(() => {
wait = false;
},ms);
}
};
};
let c = 1;
const throttled = throttle(function(i) {
console.log('callback '+i);
},1000);
let id;
const callback = function _c() {
throttled(c);
c++;
id = requestAnimationFrame(_c);
};
id = window.requestAnimationFrame(callback);
setTimeout(() => {
window.cancelAnimationFrame(id);
console.log('stopped');
},5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment