简体   繁体   English

简单的Javascript画廊

[英]Simple Javascript Gallery

So I'm trying to loop through this array and change an image source every few seconds. 所以我试图遍历这个数组并每隔几秒就改变一次图像源。 Right now I have an onload event calling a setTimeOut method which should change the image 5 seconds after the page has loaded I would think, but it is doing it instantly. 现在我有一个onload事件调用一个setTimeOut方法,该方法应该在页面加载后5秒更改图像,我会想,但它会立即执行。 What is the problem? 问题是什么? Here is my code: 这是我的代码:

<html>
<head>
    <title>Ad Rotaror</title>
    <script type="text/javascript">
        var i = 0;
        var ads = new Array(4);
                  ads[0]='promo1.gif';
                  ads[1]='promo2.gif';
                  ads[2]='promo3.gif';
                  ads[3]='promo4.gif';
                  ads[4]='promo5.gif';

        function change()
        {
            if(i > 4)
                i = 0;

            document.images[0].src = ads[i];
            i++;
        }
    </script>
</head>
<body>
    <img src="promo1.gif" onload="setInterval(change(), 5000)" />
</body>
</html>

Change 'change()' to 'change'. 将'change()'更改为'change'。 You are calling the function immediately. 您正在立即调用该函数。

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

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