与 addEventListener 的竞争条件。什么时候绑定?
我正在使用 post 消息动态调整 iframe 的高度。保存 iframe 的 html 添加一个监听器,该监听器将触发框架调整大小。父级上的(简化的)相关代码如下所示:
<div id="dynamic_content">
<iframe src="http://my.content"></iframe>-->
</div>
<script>
window.onload = function () {
window.addEventListener("message", handleEventTrigger, false);
};
function handleEventTrigger(evt) {
var height = evt.data;
alert("Received: " + evt.data);
if (isNumber(height)) {
$("#dynamic_content").height(height + "px");
}
}
function isNumber(str) {
return !isNaN(str - 0);
}
</script>
在 iframe 的源上,我在加载时触发 postMessage。基本上我有一个 id 为outerDiv 的div,我想发送它的高度。相关的javascript代码是:
window.onload = function () {
var height = $("#outerDiv")[0].offsetHeight;
if (isNumber(height)) {
parent.postMessage(height, "*");
}
};
我现在的问题是这只偶尔有效。我假设添加事件侦听器和触发发布消息之间存在竞争条件。我依赖于在发布消息被触发之前绑定事件监听器。确保这一点的适当方法是什么?两者都绑定到window.onload
。这些是否针对同一个窗口
,这是问题所在吗?
I am using post message to adjust the height of an iframe dynamically. The html that is holding the iframe adds a listener that will trigger the frame resize. The (simplified) relevant code on the parent looks something like this:
<div id="dynamic_content">
<iframe src="http://my.content"></iframe>-->
</div>
<script>
window.onload = function () {
window.addEventListener("message", handleEventTrigger, false);
};
function handleEventTrigger(evt) {
var height = evt.data;
alert("Received: " + evt.data);
if (isNumber(height)) {
$("#dynamic_content").height(height + "px");
}
}
function isNumber(str) {
return !isNaN(str - 0);
}
</script>
On the source for the iframe I trigger a postMessage when it loads. Basically I have a div with id outerDiv which I want to send the height of. The relevant javascript code is:
window.onload = function () {
var height = $("#outerDiv")[0].offsetHeight;
if (isNumber(height)) {
parent.postMessage(height, "*");
}
};
My problem now is that this only works occasionally. I am assuming there is a race condition between adding the event listener and firing the post message. I rely on having bound the event listener before the post message is fired. What is the appropriate way to ensure this? Both are bound to window.onload
. Are these addressing the same window
, and is this a/the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解,代码是按照打印的顺序执行的,因此我通过确保将
addEventListener
放置在代码中的之前解决了这个问题 -并且停止执行此加载:
这解决了问题,并且我的竞争条件消失了。但是,根据我的理解,建议将 js 代码放在 html 的末尾。如果有一个好的解决方案,我也可以实现这一点,请大声喊出来。
From what I understand the code is executed in the order it is printed, so I solved this by making sure the
addEventListener
is placed before the<iframe>
in the code - and also stop doing this onload:This solves the problem, and my race condition is gone. However, from what I've understood the recommendation is to place the js-code at the end of the html. If there is a good solution where I can achieve this too please shout out..
这只是一个猜测,但当您不发布高度而是发布回调函数名称时,可以避免竞争条件或异步计时,以便侦听器必须调用该函数来获取 iframe 的当前高度。
It's just a guess but a race condition or asynchronous timeing can be avoid when you not post the height but a callback function name so that the listener must call that function to get the current height of the iframe.