window.open多个setintervals图像循环

时间:2015-12-05 04:01:58

标签: javascript loops setinterval window.open

我的间隔时间有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>

2 个答案:

答案 0 :(得分:0)

如果您打开的窗口与您的代码所在的页面位于同一个域中,那么您可以创建一个图像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>

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

答案 1 :(得分:0)

以下是我试图为这些图像生成无限循环的内容。

<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>
相关问题