0

I have a page where I need to have 3 images, one below another, and I need to change them every 10 seconds (for example: images 1-3 are displayed, after 10 seconds images 4-6 are displayed, after 10 seconds images 7-9 are displayed, ..., images 1-3 are displayed).

For now on, I have a code, which changes only the first from the 3 images. How could I make it so it just change all of the 3 images at once, after every 10 seconds?

Here's a javascript sample:

<script>
        var links = ["http://www.example.com","http://www.def.com","http://www.ghi.com"];
        var images = ["http://www.example.com/img1.png", "http://www.example.com/img2.png", 
              "http://www.example.com/img3.png", "http://www.example.com/img4.png",
              "http://www.example.com/img5.png", "http://www.example.com/img6.png"
             ];
        var i = 0;
        var renew = setInterval(function(){
            if(links.length == i){
                i = 0;
            }
            else {
            document.getElementById("bannerImage").src = images[i]; 
            document.getElementById("bannerLink").href = links[i]; 
            i+=1;

        }
    },10000);
</script>

HTML code :

<a id="bannerLink" href="http://www.example.com" onclick="void window.open(this.href); return false;">
<img id="bannerImage" src="http://www.example.com/img1.png" width="694" height="83" alt="some text">
</a>
<a id="bannerLink" href="http://www.example.com" onclick="void window.open(this.href); return false;">
<img id="bannerImage" src="http://www.example.com/img2.png" width="694" height="83" alt="some text">
</a>
<a id="bannerLink" href="http://www.example.com" onclick="void window.open(this.href); return false;">
<img id="bannerImage" src="http://www.example.com/img3.png" width="694" height="83" alt="some text">
</a>

------------------------------------------------------------------------------------------------------------------------------------EDIT ------------------------------------------------------------------------------------------------------------------------------------

Please, take a look at the code below. I need something like this:

<head>
<title>Test</title>
    <script>
        var links = ["http://www.example.com","http://www.example.com","http://www.example.com"];
        var images = [  "https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png",
            "https://upload.wikimedia.org/wikipedia/commons/2/29/Example_image_not_be_used_in_article_namespace.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/c/ce/Example_image.png",
                "https://upload.wikimedia.org/wikipedia/commons/9/90/Contoh.jpg",
                "https://upload.wikimedia.org/wikipedia/commons/e/e2/P%C5%99%C3%ADklad.jpg"
             ];
        var i = 0;
        var renew = setInterval(function(){
            if(links.length == i){
                i = 0;
            }
            else {
            document.getElementById("bannerImage").src = images[i]; 
            document.getElementById("bannerLink").href = links[i]; 
            i+=1;

        }
        },3000);
        </script>
</head>
<a id="bannerLink" href="http://www.example.com" onclick="void window.open(this.href); return false;">
<img id="bannerImage" src="https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg"  alt="some text">
</a></br>
<a id="bannerLink" href="http://www.example.com" onclick="void window.open(this.href); return false;">
<img id="bannerImage" src="https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png" alt="some text">
</a></br>
<a id="bannerLink" href="http://www.example.com" onclick="void window.open(this.href); return false;">
<img id="bannerImage" src="https://upload.wikimedia.org/wikipedia/commons/2/29/Example_image_not_be_used_in_article_namespace.jpg" alt="some text">
</a></br>

</body>

With the above code, only the first image changes, the second and the third are the same all the time, but I need to change them too after very 10 seconds (I need to do the same thing as I did with image 1, with images 2 and 3).

4
  • Possible duplicate of Change image in HTML page every few seconds Commented May 2, 2016 at 11:41
  • ID should be unique... Commented May 2, 2016 at 11:44
  • May be you are setting only one image (bannerImage)? As your above code implies? Commented May 2, 2016 at 11:47
  • As @Victoray mentioned you have to set unique Id like bannerImage1, bannerImage2,... etc. because javascript not uses multiple same ids at same page Commented May 2, 2016 at 12:12

3 Answers 3

2

This version has links as well. Try this JS FIDDLE

<a id='bannerLink1' href="#"><img src="" id='img1' ></a>
<a id='bannerLink2' href="#"><img src="" id='img2' ></a>
<a id='bannerLink3' href="#"><img src="" id='img3' ></a>

<script>
var images = ["http://placeholdit.imgix.net/~text?txtsize=85&bg=ef4135&txtclr=ffffff&txt=IMG1&w=327&h=420", "http://placeholdit.imgix.net/~text?txtsize=85&bg=ef4135&txtclr=ffffff&txt=IMG2&w=327&h=420", "http://placeholdit.imgix.net/~text?txtsize=85&bg=ef4135&txtclr=ffffff&txt=IMG3&w=327&h=420",
              "http://placeholdit.imgix.net/~text?txtsize=85&bg=ef4135&txtclr=ffffff&txt=IMG4&w=327&h=420", "http://placeholdit.imgix.net/~text?txtsize=85&bg=ef4135&txtclr=ffffff&txt=IMG5&w=327&h=420","http://placeholdit.imgix.net/~text?txtsize=85&bg=ef4135&txtclr=ffffff&txt=IMG6&w=327&h=420","http://placeholdit.imgix.net/~text?txtsize=85&bg=ef4135&txtclr=ffffff&txt=IMG7&w=327&h=420"
             ];

var links = ["http://placeholdit.imgix.net/~text?txtsize=85&bg=ef4135&txtclr=ffffff&txt=IMG1&w=327&h=420", "http://placeholdit.imgix.net/~text?txtsize=85&bg=ef4135&txtclr=ffffff&txt=IMG2&w=327&h=420", "http://placeholdit.imgix.net/~text?txtsize=85&bg=ef4135&txtclr=ffffff&txt=IMG3&w=327&h=420",
              "http://placeholdit.imgix.net/~text?txtsize=85&bg=ef4135&txtclr=ffffff&txt=IMG4&w=327&h=420", "http://placeholdit.imgix.net/~text?txtsize=85&bg=ef4135&txtclr=ffffff&txt=IMG5&w=327&h=420","http://placeholdit.imgix.net/~text?txtsize=85&bg=ef4135&txtclr=ffffff&txt=IMG6&w=327&h=420","http://placeholdit.imgix.net/~text?txtsize=85&bg=ef4135&txtclr=ffffff&txt=IMG7&w=327&h=420"
             ];
var i = 0;
var renew = setInterval(function(){
        if(i==images.length) i=0;
        document.getElementById("img1").src = images[i]; 
        document.getElementById("bannerLink1").href=links[i];
        if(i+1==images.length) i=-1;
        document.getElementById("img2").src = images[i+1];
        document.getElementById("bannerLink2").href=links[i+1];
        if(i+2==images.length) i=-2;
        document.getElementById("img3").src = images[i+2];
        document.getElementById("bannerLink3").href=links[i+2];
        i+=3;


},1000);
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it definitely does! Thanks! Just another question: how can I add links to images, so that by clicking on image it will redirect me to the page I want?
OK, I have added the links as well. Try it now. Please accept the answer if you are satisfied.. :)
1

I have set 3 seconds interval and alert to display different image changes so please have a look, may be this will help you

<head>
<title>Test</title>
    <script>
    var images = ["img1.png","img2.png","img3.png"];
    function changeImage()
    {
        var i = 0;
        var inter = setInterval(function(){
            if(i < images.length)
            {
                //document.getElementById("bannerImage").src = images[i];
                alert(images[i]);
                i++;
            }
            else
            {
                i = 0;
                //document.getElementById("bannerImage").src = images[i];
                alert(images[i]);
                i++;
            }
        },3000);
    }
    </script>
</head>
<body onload="changeImage()">
</body>

Comments

0
setInterval(function(){
    functions here.
}, 10000);

More info here: http://www.w3schools.com/jsref/met_win_setinterval.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.