我的目标是在屏幕上显示100个左右的URL几秒钟,以便一次在同一浏览器选项卡中运行数周。
我有一个非常简单的PHP / HTML代码,可以通过meta-refresh完成此操作,但是一旦在选项卡中启动它,该选项卡将在24小时内崩溃,有时会在4-8小时内崩溃。尽管Chrome的使用时间最长,但这种情况在Chrome,Safari和Firefox上都会发生。
为什么?
https://gist.github.com/evaldasalex/5a7ee5722daeaf001b79
最佳答案
纯粹的客户端JavaScript或AJAX解决方案可能比使用meta刷新方法更适合您。
如果这些URL只是一个硬编码列表,那么您的解决方案可以完全在JavaScript方面完成;不需要PHP(提供原始HTML,CSS和JS除外)。
这是一个解决方案,它遍历URL列表并在设置的时间间隔后更新iframe:http://jsbin.com/lezuquma/1/edit?html,js,output
HTML:
<iframe id="fr" src="http://en.wikipedia.org/wiki/Main_Page"
style="border: 0; width:100%; height:100%;" sandbox="allow-same-origin allow-scripts allow-popups allow-forms"></iframe>
CSS:
html, body {
height: 100%;
width: 100%;
}
JavaScript(注意:使用jQuery):
$(document).ready(function() {
var urls = [
"http://ImportOutpost.com",
"http://blog.appannie.com/",
"http://health.ucsd.edu/specialties/mindfulness/schedule/Pages/default.aspx",
"http://www.treysmithblog.com/",
"http://en.wikipedia.org/wiki/Matrix_(mathematics)",
"http://www.cafepress.com/cp/info/sell/index.aspx?area=openashop&page=openashop",
"http://www.fiverr.com/categories/writing-translation/",
"http://mashable.com/mobile/"
];
var frame = $('#fr');
setInterval(function() {
var currentUrl = urls.shift();
urls.push(currentUrl);
frame.attr('src', currentUrl);
}, 10000);
});
如果无论出于何种原因都无法对URL进行硬编码,则只需使用AJAX从服务器到客户端检索URL,然后使用与上面用来刷新iframe相同的方法即可。
我只让我的解决方案运行了几分钟,所以如果让它连续运行几个小时,它可能仍然会崩溃。根据您选择的刷新间隔,这将是一项繁重的操作。
关于html - 为什么这个简单的代码会使所有主要浏览器的浏览器标签崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25124513/