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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:51:34+00:00 2026-06-04T16:51:34+00:00

I have an array of images that I want to loop through infinitely ie.

  • 0

I have an array of images that I want to loop through infinitely ie. 1, 2, 3, 1, 2, 3…

At first, I tried to do this using the following code:

var images = [
  "/images/image1.jpg",
  "/images/image2.jpg",
  "/images/image3.jpg"
];

var obj = { currentValue: 0 };
var maxValue = 2;            

//loop through the items
var infiniteLoop = setInterval(function() {
  if(obj.currentValue == maxValue) { 
    obj.currentValue = 0;                                           
  }

  // ... Code to fade in currentItem ...

  obj.currentValue++;
}, 5000);

I’d read that this is correct method of passing in a variable by reference but for some reason, I’m never able to set the obj.currentValue back to 0 when all the images have been looped through.

I figured an alternative way would be to set the value of an html field:

var images = [
  "/images/image1.jpg",
  "/images/image2.jpg",
  "/images/image3.jpg"
];

var maxValue = 2;            

//loop through the items
var infiniteLoop = setInterval(function() {
  if(parseInt($('#currentItem').val()) == maxValue) { 
    $('#currentItem]').val('0');                                           
  }

  //... code to fade in currentItem ...

  var tmp = parseInt($('#currentItem').val());
  tmp++;                                  
  $('#currentItem').val(tmp);
}, 5000);

<input type="hidden" id="currentItem" name="currentItem" value="0" />

However I’m still having the same problem. For some reason, whenever I hit the end of the image list, I’m unable to set the value of the hidden field and my infinite loop never gets to restart.

Am I missing something obvious here? I can’t seem to figure out how to get this working.

If anyone has a more efficient method of achieving this I’d also be very grateful if you could share it 🙂

Thanks

  • 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-04T16:51:35+00:00Added an answer on June 4, 2026 at 4:51 pm

    So I rewrote your js a bit, with comments on how/why to do things

    // Give your code a unique namespace using an immediately/self invoked annonymous function
    (function (window) {
    
        // For better management we could use some more variables
        // slider.images.length replaces maxvalue
        var slider = { 
            images: [
              "/images/image1.jpg",
              "/images/image2.jpg",
              "/images/image3.jpg"
            ], 
            current: 0, // name your variables semantically, we know this is going to have a value so don't 'append' things to the name that are obvious
            time: 5000
        };
    
        // separated the function so we DRY
        function rotate() {
            // Remember that the images array is 0 indexed and length gives the total amount of 
            // items in the array which will be one more, if they're the same then we reset 
            // current to 0
            if(slider.current == slider.images.length)
                slider.current = 0;
    
            // Code to do w/e
            console.log(slider.images[slider.current], slider.current);
    
            slider.current++;
            window.loop = setTimeout(rotate, slider.time);
        }
    
        // only thing about intervals really, they never stop, and can never be stopped
        // so better thing to do is use a recursive timeout, and ideally it should be available 
        // somehow so you can stop it outside of the script itself, in this case we put the 
        // reference on window. (which is not ideal, anywhere else it makes sense is better)
        window.loop = setTimeout(rotate, slider.time);
    
    }( window ));
    ​
    

    And here in jsFiddle:

    http://jsfiddle.net/sg3s/RGQxY/5/

    You’re using jquery to do things, that is fine, but you’ll have to pass jQuery to be able to use it inside that immediately invoked function that wrapper could look something like this: (function ($, window) { /* code goes here */ }( jQuery, window ));.

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

Sidebar

Related Questions

I am using PHP and I have an array of user images that I
I have image Array with two images out of that first image its showing
I have an array of images that I want to display in a random
How do i loop this array, so that I do not have to write
I need to get array of images that have to call from server and
I have a service that converts images stored on a website to byte array
I have an array that is taken from an image using exif_read_data($image, 0, true)
I have an array of urls of images, from which i have to first
I have an array of images. Each image has a rarity key that tells
I have a while loop that goes through a table and I echo the

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.