Skip to content

Instantly share code, notes, and snippets.

@danro
Last active May 6, 2024 05:18
Show Gist options
  • Save danro/7846358 to your computer and use it in GitHub Desktop.
Save danro/7846358 to your computer and use it in GitHub Desktop.
underscore throttle using requestAnimationFrame
// Returns a function, that, when invoked, will only be triggered once every
// browser animation frame - using tram's requestAnimationFrame polyfill.
// tram.js - https://github.com/bkwld/tram
_.throttle = function(func) {
var wait, args, context;
return function () {
if (wait) return;
wait = true;
args = arguments;
context = this;
window.tram.frame(function () {
wait = false;
func.apply(context, args);
});
};
};
@yairEO
Copy link

yairEO commented Aug 2, 2015

it's not a throttle function if you can't control the time of callback firing.

it's more of a requestAnimationFrame which keeps calling itself..

@towry
Copy link

towry commented Jun 21, 2016

yairEO is right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment