Skip to content

Instantly share code, notes, and snippets.

@jnsprnw
Created December 3, 2015 13:31
Show Gist options
  • Save jnsprnw/403494c637195accf151 to your computer and use it in GitHub Desktop.
Save jnsprnw/403494c637195accf151 to your computer and use it in GitHub Desktop.
Throttle window resize event
import { throttle } from 'lodash';
function addEvent(object, type, callback) {
if (object === null || object === undefined) return;
if (object.addEventListener) {
object.addEventListener(type, callback, false);
} else if (object.attachEvent) {
object.attachEvent('on' + type, callback);
} else {
object['on' + type] = callback;
}
}
function getWindowSize() {
const el = document.getElementById('content');
const width = el.offsetWidth;
const height = el.offsetHeight;
// Do stuff with width and height
}
addEvent(window, 'resize', throttle(getWindowSize, 300));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment