window.打开多个设置间隔的图像循环

window.open multiple setintervals images loops

本文关键字:图像 循环 设置 window      更新时间:2023-09-26

我的Intervals有3个问题。

一个是我的内部只显示两张图片,而我正试图显示4张。我试图复制我的函数,并将<img />更改为doc.write(didn't work)

最后,我如何从这些间隔中创建一个循环(显示1,2,3,4)nostop。它需要将所有图像显示到window.open中。(有什么建议吗:创建一个<img />数组更好吗?)

<script type="text/javascript" >
    var OpenWindow = window.open("http://vrjournal.com/adtech-new-york-showcases-virtual-realitys-move-into-mobile-advertising","","top=100, left=400,resizable=yes,height=550,width=550,menubar=yes,location=yes,resizable=yes,scrollbars=yes");
    Update1();
    function Update1() {
        OpenWindow.document.write("<IMG float:'center' SRC='Oculus.jpg'>");
        setInterval("Update2();",3000);
    }
    function Update2() {
        OpenWindow.document.write("<IMG float:'center' SRC='future-vr.jpg'>");
        setInterval("Update1();",3000);
    }
</script>

如果您打开的窗口与代码所在的页面位于同一个域上,那么您可以创建一个图像URL数组,并在每个计时器上对URL进行索引。

<script type="text/javascript" >
    var OpenWindow = window.open("http://vrjournal.com/adtech-new-york-showcases-virtual-realitys-move-into-mobile-advertising","","top=100, left=400,resizable=yes,height=550,width=550,menubar=yes,location=yes,resizable=yes,scrollbars=yes");
    var imgURLS = ['Oculus.jpg', 'future-vr.jpg', 'img3.jpg', 'img4.jpg'];
    var imgIndex = 0;
    var timer = setInterval(function() {
        if (imgIndex >= imgURLS.length) {
            // done with the array of images, stop the timer
            clearInterval(timer);
        } else {
            OpenWindow.document.write('<img src ="' + imgURLS[imgIndex++] +  '">');
        }
    }, 3000);
</script>

如果您打开的窗口与代码所在的页面位于不同的域中,则由于同源安全保护,浏览器将不允许您修改其他窗口。

以下是我试图为这些图像生成一个无限循环的操作。

<script type="text/javascript" >
        var OpenWindow = window.open("","","top=100, left=400,resizable=yes,height=550,width=550,menubar=yes,location=yes,resizable=yes,scrollbars=yes");
        var imgURLS = ['Oculus.jpg', 'future-vr.jpg', 'morpheus.jpg', 'samsungvr.jpg'];
        var imgIndex = {
      for (var i = 0; i < imgURLS.length; i++) { 
        for (var j = 0; j < imgURLS.length; j++)
    };
        var timer = setInterval(function() {
            if (imgIndex > imgURLS.length) {
                clearInterval(timer);
            } else {
                OpenWindow.document.write('<img src ="' + imgURLS[imgIndex++] +  '">');
            }
        }, 3000);
        </script>