php - how to change iframe different src after every 5 second

雨燕双飞 提交于 2019-12-02 17:41:51

问题


i have tried following code but it could change on click, i want to change the iframe inner part after every 5 second interval.

<html>
 <body>
  <iframe id="foo"></iframe>
  <a href="http://www.mydomain.com" target="foo">This page</a>
 </body>
</html>

basically idea is that i have pages which will display inside of iframe, these 5 web pages will change after another after every 5 second interval.

thanks


回答1:


Based on @Matthew Dean answer :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Fouad Ali</title>
<script>
var links = ["http://www.example.com/page","http://www.example.com/anotherpage"];
var i = 0;
var renew = setInterval(function(){
    document.getElementById("foo").src = links[i];        
    if(links.length == i){
        i = 0;
    }
    else i++;
},5000);
</script>
</head>

<body>
<iframe id="foo" src="http://example.com"></iframe>
</body>
</html>

Important: Not all sites can be used within iframes. Be sure that the website you put in the links array allows to be used inside an iframe.




回答2:


This will do it:

var urls = ["http://www.google.com", "http://www.example.com"];
var i = 0;

function changeSrc() {
   if (urls.length > i) {
      document.getElementById("foo").src = urls[i];
      i++;
      setTimeout(function() {
         changeSrc();
      }, 5000);
   }
}
changeSrc();



来源:https://stackoverflow.com/questions/13655598/php-how-to-change-iframe-different-src-after-every-5-second

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!