[英]Call function after detecting iFrame has loaded, or after 3 second timeout
I want to load a external URL in a hidden iframe on my web page. 我想在网页上的隐藏iframe中加载外部URL。 I need to detect once the external URL has finished loading, and then call my Function MyFunct().
一旦外部URL完成加载,我就需要检测,然后调用我的函数MyFunct()。 If the external URL takes longer than 3 seconds to load I need to timeout and call MyFunct()
如果外部URL的加载时间超过3秒,则需要超时并调用MyFunct()
Will the following code call my function MyFunct() either once the iframe has loaded or after 3 seconds because of the timeout ? 加载iframe后或由于超时3秒后,以下代码会调用我的函数MyFunct()吗?
var iframe = document.getElementById('MyFrame');
iframe.src = "SomeRandonUrl";
iframe.onload = function () {
setTimeout(MyFunct, 3000);
}
You should check if it has loaded in a timeout event like the following. 您应该检查它是否已加载超时事件,如下所示。
var iframe = document.getElementById('MyFrame');
iframe.src = "SomeRandonUrl";
iframe.onload = function () {
if (iframe.src !== "about:blank")
iframe.loaded = true; // set that it has loaded if not about:blank
}
setTimeout(function() {
if (!iframe.loaded) { // if undefined or false valued
alert('Timedout');
// and then you can cancel it's loading setting another src for e.g
}
}, 3000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.