Created
December 3, 2015 13:31
-
-
Save jnsprnw/403494c637195accf151 to your computer and use it in GitHub Desktop.
Throttle window resize event
This file contains 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 { 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