简体   繁体   English

在循环中打开多个 windows

[英]Open multiple windows in loop

I am having issues opening multiple windows in a correct order, no matter what I try it opens all windows simultaniously - which can be serveral hundred.我在以正确的顺序打开多个 windows 时遇到问题,无论我尝试什么,它都会同时打开所有 windows - 这可能是几百个。 I am trying to open one window, do something, close window, repeat until all have opened.我正在尝试打开一个 window,做一些事情,关闭 window,重复直到全部打开。

var mission = document.querySelectorAll('a[href*="/missions"]');
var i;

document.getElementById("sharebtnalliance").onclick = function shareMission(){
    for(i = 0; i < mission.length; i++){
        window.open(mission[i], "", "height: 80%; width: 80%;");
        setTimeout(function() {
            window.close();
        }, 5000);
    }
}

The delay does not seem to kick in, 5 seconds would be long enough to do what needs doing before opening the next URL but it also does not close them - could be because the browser is struggling to keep up with so many opening pages.延迟似乎没有开始,5 秒足以在打开下一个 URL 之前完成需要做的事情,但它也没有关闭它们 - 可能是因为浏览器正在努力跟上这么多打开的页面。

I personally wouldn't use a timeout, you should probably wait for the window to be closed with something like this: NB This will only work if the URLs are on the same domain.我个人不会使用超时,您可能应该等待 window 关闭,如下所示: 注意这仅在 URL 位于同一域中时才有效。

CAVEAT: THIS HAS NOT BEEN TESTED AND MAY REQUIRE SOME DEBUGGING警告:这尚未经过测试,可能需要进行一些调试

 var mission = document.querySelectorAll('a[href*="/missions"]'); document.getElementById("sharebtnalliance").onclick = async function shareMission(){ for(let i = 0; i < mission.length; i++){ await openWindowAsync(mission[i]); } } function openWindowAsync(url){ return new Promise(resolve => { const w = window.open(url, "", "height: 80%; width: 80%;"); w.addEventListener('close', resolve); }); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM