每 20 秒更改 iframe 的 url 不适用于 javascript
我有一个 iframe,我想每 20 秒更新一次。所以基本上,iframe 的 url 应该从数组中更改为新的,并且每 20 秒刷新一次,但我的代码似乎不起作用。
var Uni = {
init: function () {
this.refresh();
},
refresh: function () {
var urlArr = [
'http://www.smashingmagazine.com/learning-javascript-essentials-guidelines-tutorials/',
'http://stephendnicholas.com/archives/310',
'http://msdn.microsoft.com/en-us/scriptjunkie/ff696765',
'http://www.jquery4u.com/jquery-functions/jquery-each-examples/'
],
iframeSrc = document.querySelector('#uni iframe').src;
for (var i = 0, max = urlArr.length; i < max; i++) {
setInterval(function(){
iframeSrc = urlArr[i];
}, 20000);
}
}
}
Uni.init();
HTML:
<body id="uni">
<iframe src="http://www.smashingmagazine.com/learning-javascript-essentials-guidelines-tutorials/">
You browser doesn't support iframes, please update it.
</iframe>
</body>
请帮忙。
I have an iframe that I wanna update every 20 seconds. So basically, the url of the iframe should change to a new one from the array and it refreshes, every 20 seconds but my code doesn't seem to work.
var Uni = {
init: function () {
this.refresh();
},
refresh: function () {
var urlArr = [
'http://www.smashingmagazine.com/learning-javascript-essentials-guidelines-tutorials/',
'http://stephendnicholas.com/archives/310',
'http://msdn.microsoft.com/en-us/scriptjunkie/ff696765',
'http://www.jquery4u.com/jquery-functions/jquery-each-examples/'
],
iframeSrc = document.querySelector('#uni iframe').src;
for (var i = 0, max = urlArr.length; i < max; i++) {
setInterval(function(){
iframeSrc = urlArr[i];
}, 20000);
}
}
}
Uni.init();
HTML:
<body id="uni">
<iframe src="http://www.smashingmagazine.com/learning-javascript-essentials-guidelines-tutorials/">
You browser doesn't support iframes, please update it.
</iframe>
</body>
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查urlArr[]
声明的最后一行:感谢@AndyE 的提示。更新:解决方案+工作小提琴
Check the last line of yoururlArr[]
declaration:Thanks to @AndyE for the hint.Update: Solution + working fiddle
iframe src 属性只是一个字符串,而不是指针。改变它没有任何效果。
用这个代替:
The iframe
src
attribute is only a string, not pointer. Changing it has no effect.Have this instead: