Jump to content

[SOLVED] Change image after some time


Allan-

Recommended Posts

I want to change image src after some time to another (after 3 secs in example) and if there arent more images then turn into the first one again. In example I have 3 different image sources (gfx/img1.jpg, gfx/img2.jpg, gfx/img3.jpg).

 

This is part of my code but something is wrong there. Can someone help?

 

   <script type="text/javascript">
      var i = 0;
      var image = document.getElementById('changeimage');
      function changeImage() {
         if (i == 0) {
            image.src = 'gfx/img1.jpg';
            i = 1;
         } else if (i == 1) {
            image.src = 'gfx/img2.jpg';
            i = 2;
         } else {
            image.src = 'gfx/img3.jpg';
            i = 0;
         }
         setTimeout('changeImage()', 3000);
      }

   </script>

</head>

<body onload="changeImage();">
   <img id="changeimage" src="gfx/img1.jpg" />

Link to comment
Share on other sites

What error do you get in Firefox's error console?

 

I don't think you can create an object like this outside of a function:

 

var image = document.getElementById('changeimage');

 

Try adding it inside the function..

 

A

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.