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 8318101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:50:28+00:00 2026-06-08T21:50:28+00:00

So on my new website in google chrome, I am trying to make the

  • 0

So on my new website in google chrome, I am trying to make the images switch every 5 seconds using a setInterval function. This seems to work, however I have the problem cannot set property src of null.

var images = new Array();
images[0] = "/images/grilled-chicken-large.png";
images[1] = "/images/sizzly-steak.png";
var img = document.getElementById("img");

function changeImage() {
     for (var i = 0; i < 3; i++) {
        img.src = images[i];
        if (i == 2){i = i - 2;};
     }
}

window.onload=setInterval("changeImage()", 5000);

I know the problem is that I am trying to get the element with an id of img before the page is done loading, but I’ve already got a window.onload, so i don’t know what to do. Also, if i make an init() function containing the setInterval and the img variable, the page freezes.

Any ideas?

  • 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-08T21:50:31+00:00Added an answer on June 8, 2026 at 9:50 pm

    The problem is that you access element 2 of an array with only two elements (0 and 1).

    But there is also a logical error in your script: Where do you store the index of the currently visible image? You need a global variable which holds that index.

    var currentIndex = 0;
    function changeImage() {
        // Switch currentIndex in a loop
        currentIndex++;
        if (currentIndex >= images.length)
            currentIndex = 0;
    
        var img = document.getElementById("img");
        img.src = images[currentIndex];
    }
    
    document.onload = function() {
        setInterval(changeImage, 5000);
    }
    

    I’ve moved the img variable into the function so that it is assigned after five seconds when the document has loaded.

    Just to clear things up: JavaScript is an event-based programming language. That means that the slideshow-change code is executed every time the interval fires, does some work (switch to the next slide) and then is done, so that the browser can render the page. If you iterate through the slides in a loop, there is no way for the browser to show the new slide because the script is still executing between the slides. The solution is what I’ve posted: Define a global variable that replaces the loop variable, and increment it every time the next slide is to be shown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a website using Google API V3 to show your
I am planning a new website with codeigniter using wordpress. this site will contain
I'm trying to make a nice-looking CSS menu, for this website . (The domain
Hi, I'm trying to make a website with Google Maps API and get coords
So I'm trying to use this javascript with my asp.net website https://google-developers.appspot.com/chart/interactive/docs/gallery/linechart I have
Planning to create a new website for our product using ASP.NET MVC 4. Site
I am writing a new website that uses PHP and MySQL. I am trying
I've got a new website moved to my server. This website uses PHP and
I've got the following Google map on my website.. function initialize() { if (GBrowserIsCompatible())
I'm using <mx:LinkButton label=www.google.com click=navigateToURL(new URLRequest(event.currentTarget.label.toString())) /> to open a browser window to display

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.