在一定延迟后逐个加载不同的URL [英] load different urls one by one after certain delay
问题描述
您好我有一系列网址,我想逐个加载到同一个浏览器窗口中。现在,我可以使用
Hi I have a array of urls which I want to load in the same browser window one by one. For now I am able to load a single url in a browser window using
window.location = url;
但是当我把这个window.location放在一个函数中并且在一个循环中无限地调用该函数时延迟页面只是继续加载,没有任何东西显示。代码如下:
But when I put this window.location in a function and call that function infinitely in a loop with some delay the page just keeps on loading and nothing gets displayed. Code is below:
<html>
<head>
<script>
var urls = ["http://www.howstuffworks.com", "http://example.com"];
var i = 0;
function redirect_url(url)
{
window.location = url;
}
while (true)
{
setTimeout(function() { redirect_url(urls[i]); }, 5000);
// update the index
i = (i + 1) % urls.length;
}
</script>
</head>
我想要的是什么时候我在我的浏览器中打开这个脚本文件然后第一个url的网页应该被加载,并且在5秒之后第二个url的网页应该被加载,这应该无限地继续。
What I want is when I open this script file in my browser then the first url's webpage should get loaded and after 5 secs second url's webpage should get loaded and this should continue infinitely.
推荐答案
这是一个没有While循环的方法;似乎在我的最后工作: http://jsfiddle.net/77617znz/2/
Here is a method without the While loop; Seems to work well on my end: http://jsfiddle.net/77617znz/2/
var urls = ["http://www.howstuffworks.com", "http://example.com"];
var i = 0;
function redirect_url(){
if( i <= urls.length ){
setTimeout(function(){
$('iframe').attr('src', urls[i]);
i++;
redirect_url();
}, 3000);
}
}
redirect_url();
希望这会有所帮助!如果您有任何问题,请告诉我。
Hope this helps! Let me know if you have any questions.
这篇关于在一定延迟后逐个加载不同的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!