Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9011311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:41:46+00:00 2026-06-16T02:41:46+00:00

I want to change images every few seconds, this is my code: <?xml version

  • 0

I want to change images every few seconds, this is my code:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
   <head>
      <title>change picture</title>
      <script type = "text/javascript">

    function changeImage()
    {
    var img = document.getElementById("img");
    img.src = images[x];
    x++;

    if(x >= images.length){
        x = 0;
    } 
   var timerid = setInterval(changeImage(), 1000);
}   }
var images = [], x = 0;
images[0] = "image1.jpg";
images[1] = "image2.jpg";
images[2] = "image3.jpg";

      </script>
   </head>
   <body onload = "changeImage()">
 <img id="img" src="startpicture.jpg">
   </body>
</html>

My problem is its stuck on the first picture!

I also wanted to try flipping through the pictures with previous and next buttons but I have no idea how to do that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:41:47+00:00Added an answer on June 16, 2026 at 2:41 am

    As I posted in the comment you don’t need to use both setTimeout() and setInterval(), moreover you have a syntax error too (the one extra }). Correct your code like this:

    (edited to add two functions to force the next/previous image to be shown)

    <!DOCTYPE html>
    
    <html>
       <head>
          <title>change picture</title>
          <script type = "text/javascript">
              function displayNextImage() {
                  x = (x === images.length - 1) ? 0 : x + 1;
                  document.getElementById("img").src = images[x];
              }
    
              function displayPreviousImage() {
                  x = (x <= 0) ? images.length - 1 : x - 1;
                  document.getElementById("img").src = images[x];
              }
    
              function startTimer() {
                  setInterval(displayNextImage, 3000);
              }
    
              var images = [], x = -1;
              images[0] = "image1.jpg";
              images[1] = "image2.jpg";
              images[2] = "image3.jpg";
          </script>
       </head>
    
       <body onload = "startTimer()">
           <img id="img" src="startpicture.jpg"/>
           <button type="button" onclick="displayPreviousImage()">Previous</button>
           <button type="button" onclick="displayNextImage()">Next</button>
       </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to change the backgroundImage of html with radio button selection. Every radio
I have a website and I want an image to change every 5 seconds,
i have many div's with the same background image and i want change this
This is my very first attempt at generating images on the fly. I want
I want an image to change every second. I'm having trouble with setInterval. Could
I want to change taxt value from loop and every time I have changed
I want to change the opacity of images in php. I used Imagick and
So I have a program, this I want this program to change the background
I am using this code for displaying Images in scroll view... In this buttons
So I have the HTML like this: <p class=agendaNav><img class=rightArrow src=/images/arrowright.png> WEEK AT A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.