Created
September 25, 2018 19:45
-
-
Save dtipson/a3786bcd7c81fa56114fdd858db21fa8 to your computer and use it in GitHub Desktop.
Sending postMessage events
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
function throttle(fn, threshhold = 250, scope) { | |
var last, | |
deferTimer; | |
return function () { | |
var context = scope || this; | |
var now = +new Date, | |
args = arguments; | |
if (last && now < last + threshhold) { | |
// hold on to it | |
clearTimeout(deferTimer); | |
deferTimer = setTimeout(function () { | |
last = now; | |
fn.apply(context, args); | |
}, threshhold); | |
} else { | |
last = now; | |
fn.apply(context, args); | |
} | |
}; | |
} | |
function sendHeight(){ | |
var $container = document.querySelector('body > .container'); | |
if($container && $container.offsetHeight){ | |
parent.postMessage($container.offsetHeight, "*"); | |
} | |
} | |
if(parent.postMessage){ | |
addEventListener('resize', throttle(sendHeight, 100)); | |
addEventListener("DOMContentLoaded", sendHeight); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment