Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 等待帧集中的其他帧完成加载_Javascript_Html - Fatal编程技术网

Javascript 等待帧集中的其他帧完成加载

Javascript 等待帧集中的其他帧完成加载,javascript,html,Javascript,Html,我正在尝试使用javascript对不同的框架进行一些更改,但我需要等到它被正确加载 我有frame-B,它对frame-A的内容进行了一些更改。我在frame-A中设置了一个标志,当它完成加载时: 框架-A: // Flag to indicate that the page is loaded. Used by frame-B var documentLoaded = false; $(document).ready(function () { documentLoaded = true;

我正在尝试使用javascript对不同的框架进行一些更改,但我需要等到它被正确加载

我有frame-B,它对frame-A的内容进行了一些更改。我在frame-A中设置了一个标志,当它完成加载时:

框架-A:

// Flag to indicate that the page is loaded. Used by frame-B
var documentLoaded = false;
$(document).ready(function () { documentLoaded = true; });
框架B:

function onLeftFramesLoad(loops) {
    // Check if the menu frame is finished loading, if not try again in Xms.
    // To Avoid eternal loop if for some reason the documentLoaded flag is not set after Y seconds: break loop.
    if (!parent.frames[0].window || !parent.frames[0].window.documentLoaded && 
        loops < 40)
    {
        setTimeout(onLeftFramesLoad(loops + 1), 250);
        return;
    }
    // do changes to frame-A
}

// Using jQuery here to wait for THIS frame to finish loading.
$(document).ready(function() {
        onLeftFramesLoad(0);
});

在FF和铬中测试

必须将函数传递给
setTimeout
。您当前正在立即调用函数并传递返回值(没有)

因此,您需要将其包装成一个函数,并传递该函数:

setTimeout(function() { onLeftFramesLoad(loops + 1); }, 250);
它的作用是执行onLeftFramesLoad(loops+1)的返回值,因此它在setTimeout之前执行onLeftFramesLoad。这与写作基本相同:

setTimeout(undefined, 250); // onLeftFramesLoad always returns undefined
显然,undefined()不起作用。正确的方法是

setTimeout(function() {
  onLeftFramesLoad(loops + 1);
}, 250);
因为这是一个函数,因此是可执行的


有关设置超时功能的更多信息,请查看

*Headpolm!应该看到那个。。。谢谢,非常感谢!
setTimeout(undefined, 250); // onLeftFramesLoad always returns undefined
setTimeout(function() {
  onLeftFramesLoad(loops + 1);
}, 250);