简体   繁体   English

如何在 5 分钟后将 iframe 数据自动更改为数组中的另一个 url?

[英]How to auto change iframe data after 5 minutes to another url in an array?

I mostly can't get the edit iframe part when the function is called.当调用 function 时,我大多无法获得编辑 iframe 部分。 And i want it to edit the embed to a random one of the urls in the 'responses' array.我希望它编辑嵌入到“响应”数组中的一个随机 URL。

This is what i have so far:这是我到目前为止所拥有的:

<iframe id="change"></iframe>
    <script>
var changez = document.getElementById("change")
setInterval(function(){ 
       var responses = ["/radio/kfok",    "/radio/rr", "/radio/ltunes", "/radio/alt947", "/radio/v1011", "/radio/1079kiss", "/radio/937river"]
       var rand =  responses[Math.floor(Math.random() * responses.length)];
       changez.replace(rand)
}, 300000);
    </script>

Just change this:只需更改此:

changez.replace(rand)

to this:对此:

changez.src=rand

Also, the code you've referenced is old, var is not really used anymore and should be replaced with let or const: https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/此外,您引用的代码很旧,var 不再真正使用,应该用 let 或 const 替换: https://www.freecodecamp.org/news/var-let-and-const-whats-the-区别/

This works:这有效:

    <iframe id="change" style="border:0px #ffffff none;" name="myiFrame" scrolling="no" frameborder="0" marginheight="0px" marginwidth="0px" height="100%" width="100%" allowfullscreen></iframe>
    <script>
var changez = document.getElementById("change")
setInterval(function(){ 
       var responses = ["/radio/kfok",    "/radio/rr", "/radio/ltunes", "/radio/alt947", "/radio/v1011", "/radio/1079kiss", "/radio/937river", "/radio/cgr"]
       var rand =  responses[Math.floor(Math.random() * responses.length)];
       changez.src=rand
}, 300000);
        // , "/radio/"
        function start() {
                   var responses = ["/radio/kfok",    "/radio/rr", "/radio/ltunes", "/radio/alt947", "/radio/v1011", "/radio/1079kiss", "/radio/937river", "/radio/cgr"]
       var rand =  responses[Math.floor(Math.random() * responses.length)];
       changez.src=rand
        }
        start()
    </script>

Thanks to MonsterBasket for the base code.感谢 MonsterBasket 的基本代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM