问题描述
有人可以告诉我如何使我的URL正确循环到我创建的window.open
中吗?我想知道循环是否是使每个URL根据我的setInterval
旋转的正确答案?如果是,我想知道循环是否需要在var rotate = []{for (var i = 0; i < urls.length; i++)};
下.
Can someone tell me how to make my URLs properly loop into the window.open
I created? I’m wondering if the loop is the right answer to make each URL rotate based on my setInterval
? If yes, I was wondering if the loop needs to be under var rotate = []{for (var i = 0; i < urls.length; i++)};
.
var urls = ["http://www.espn.com","http://www.disney.com","http://www.codingforums.com"];
var rotate = 0;
function goRandom()
{
var newwin = window.open(
urls[ rotate ],
"POPUP",
"height=400,width=600,scrollbars=yes"
);
}
var loop = setInterval(goRandom, 5000);
推荐答案
只需更改
urls[ rotate ],
到
urls[ (rotate++) % urls.length ],
索引在每次迭代时递增,并且% urls.length
(%用于除法后获得余数,因此可确保结果永远不会超出数组大小).您可以在此处尝试(请记住允许弹出窗口).
The index is incremented each iteration and % urls.length
(% is used for getting the remainder after division, hence it ensures the result never grows beyond the array size). You can try it here (remember to allow popups).
这篇关于循环弹出窗口混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!