Skip to content

Instantly share code, notes, and snippets.

@StanShumsky
Forked from vladnicula/window-resize.js
Last active August 29, 2015 14:17
Show Gist options
  • Save StanShumsky/6f33fe65bef74e96d97c to your computer and use it in GitHub Desktop.
Save StanShumsky/6f33fe65bef74e96d97c to your computer and use it in GitHub Desktop.
var $window = $(window),
resizeInCooldown = false,
resizeTimeout;
function onResize() {
var resizeValue;
// only run this if we didn't run a update 150ms ago or less.
if ( !resizeInCooldown ) {
// orientation dictates what height and what width means.
if (!window.orientation || window.orientation === -90 || window.orientation === 90) {
resizeValue = {
width : $window.width(),
height : $window.height()
};
} else {
resizeValue = {
height : $window.width(),
width : $window.height()
};
}
// trigger the event that should be listened to. This is what you would listen to.
$("body").trigger("resized:widnow", [resizeValue]);
// start the "in cooldown" functioanlity. This is what we check for at the beginning of the function
// to make sure we don't run this code to many times.
resizeInCooldown = true;
// clear the in cooldown
setTimeout( function () {
resizeInCooldown = false;
}, 150);
} else {
// if we were in cooldown, we wait a while longer and try to resize again. This is probably
// not the best approach, but it sort of works.
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout( function () {
onResize();
}, 200);
}
}
$window.on('resize', onResize);
$window.on('orientationchange', function () {
$window.trigger("resize");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment