Created
February 2, 2016 19:27
-
-
Save MeoMix/fba5f09904fae8bf4a90 to your computer and use it in GitHub Desktop.
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
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); | |
}); | |
} | |
}; | |
} | |
}); |
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
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