javascript开放&;使用setInterval关闭多个url

javascript开放&;使用setInterval关闭多个url,javascript,setinterval,Javascript,Setinterval,我尝试在javascript中打开多个url,并在特定时间内自动关闭它们。我没有编程方面的背景知识,只有一点php。我这样做是为了演示一个项目 我有一个带有url的数组 var allURL = ["http://google.com", "http://yahoo.com", "http://msn.com"]; 现在,我想在新窗口/选项卡中逐个打开所有url,持续10秒,然后自动关闭。所以打开10秒,自动关闭,然后打开。与数组中的所有url类似 请您指导我如何使用setInterval或任

我尝试在javascript中打开多个url,并在特定时间内自动关闭它们。我没有编程方面的背景知识,只有一点php。我这样做是为了演示一个项目

我有一个带有url的数组

var allURL = ["http://google.com", "http://yahoo.com", "http://msn.com"];
现在,我想在新窗口/选项卡中逐个打开所有url,持续10秒,然后自动关闭。所以打开10秒,自动关闭,然后打开。与数组中的所有url类似

请您指导我如何使用setInterval或任何其他方法实现这一点

var allURL=[”http://google.com","http://yahoo.com","http://msn.com"];
var allURL = ["http://google.com","http://yahoo.com","http://msn.com"];

function showUrl(index) {
    index = index || 0;

    // are there any urls to show?
    // is the given index valid?
    if (allURL.length === 0 || index < 0 || index >= allURL.length) {
        return;
    }

    // open the url
    var popup = window.open(allURL[index]);

    // set a timer which closes the popup after 10 seconds and opens the next url by calling 'showUrl' with the next index
    setTimeout(function() {
        popup.close();
        showUrl(index + 1);
    }, 10000);
}

// To start the "diashow" call 'showUrl' without an index or if you want to start at a pre-defined url with the corresponding index
showUrl();    // starts with the first url
函数showUrl(索引){ 指数=指数| | 0; //有任何URL要显示吗? //给定的索引有效吗? if(allURL.length==0 | | index<0 | | index>=allURL.length){ 返回; } //打开url var popup=window.open(allURL[index]); //设置一个计时器,该计时器在10秒后关闭弹出窗口,并通过使用下一个索引调用“showUrl”打开下一个url setTimeout(函数(){ popup.close(); showUrl(索引+1); }, 10000); } //要启动“diashow”,请在没有索引的情况下调用“showUrl”,或者如果要启动具有相应索引的预定义url showUrl();//从第一个url开始
var allURL=[”http://google.com","http://yahoo.com","http://msn.com"];
函数showUrl(索引){
指数=指数| | 0;
//有任何URL要显示吗?
//给定的索引有效吗?
if(allURL.length==0 | | index<0 | | index>=allURL.length){
返回;
}
//打开url
var popup=window.open(allURL[index]);
//设置一个计时器,该计时器在10秒后关闭弹出窗口,并通过使用下一个索引调用“showUrl”打开下一个url
setTimeout(函数(){
popup.close();
showUrl(索引+1);
}, 10000);
}
//要启动“diashow”,请在没有索引的情况下调用“showUrl”,或者如果要启动具有相应索引的预定义url
showUrl();//从第一个url开始

谢谢你的帮助。这就像我想要的一样完美。仅在chrome中,它在新窗口中打开,而不是在新选项卡中打开。是否可以强制在选项卡中打开。如果弹出窗口作为新选项卡或窗口打开,则由浏览器定义。感谢您的帮助。这就像我想要的一样完美。仅在chrome中,它在新窗口中打开,而不是在新选项卡中打开。是否可以强制在选项卡中打开。如果弹出窗口是作为浏览器定义的新选项卡或窗口打开的