Skip to content

Instantly share code, notes, and snippets.

@MeoMix
Created February 2, 2016 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MeoMix/fba5f09904fae8bf4a90 to your computer and use it in GitHub Desktop.
Save MeoMix/fba5f09904fae8bf4a90 to your computer and use it in GitHub Desktop.
import { mixin } from 'lodash';
mixin({
// Inspired by: https://gist.github.com/danro/7846358
// Leverage requestAnimationFrame for throttling function calls instead of setTimeout for better perf.
throttleFramerate: function(callback) {
var wait = false;
var args = null;
var context = null;
return function() {
if (!wait) {
wait = true;
args = arguments;
context = this;
requestAnimationFrame(function() {
wait = false;
callback.apply(context, args);
});
}
};
}
});
initialize(){
// Provide a throttled version of _onWindowResize because resize events can fire at a high rate.
// https://developer.mozilla.org/en-US/docs/Web/Events/resize
// It's important to bind pre-emptively or attempts to call removeEventListener will fail to find the appropriate reference
this._onWindowResize = _.throttleFramerate(this._onWindowResize.bind(this));
window.addEventListener('resize', this._onWindowResize);
}
onBeforeDestroy(){
window.removeEventListener('resize', this._onWindowResize);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment