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

  • SEARCH
  • Home
  • 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 9088817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:59:22+00:00 2026-06-16T21:59:22+00:00

PLEASE DO NOT RECOMMEND JQUERY – I AM DOING THIS EXERCISE FOR LEARNING PURPOSES.

  • 0

PLEASE DO NOT RECOMMEND JQUERY – I AM DOING THIS EXERCISE FOR LEARNING PURPOSES.

I have implemented a JavaScript, which rotates images (_elementSlideChange) on a timer, using a set interval of 10 seconds. Also I have added a slide functionality to this, which is 7 milliseconds (_slideImage).

The image rotates automatically every 10 seconds on page load, and I have also provided next and previous buttons, which allow the user to change the images manually.

_elementSlideChange: function () {
  var myString;
  var myText;
  for (var i = 0; i < this._imgArray.length; i++) {
    var imageArr = "url(" + this._imgArray[i].src + ")";
    var imageBg = this._imageHolder.style.background + "";
    if (imageArr == imageBg) {
      if (i == (this._imgArray.length - 1)) {
        myString = "url(" + this._imgArray[0].src + ")";
        myText = this._infoArray[0];
      } else {
        myString = "url(" + this._imgArray[(i + 1)].src + ")";
        myText = this._infoArray[i + 1];
      }
    }
  }
  this._imageNextSlide.style.background = myString;
  this._imageNextSlide.style.background);
  this._infoElement.innerHTML = myText;
  this._myTimer = setInterval(MyProject.Utils.createDelegate(this._slideImage, this),  7);
},
_slideImage: function () {
  if (parseInt(this._imageHolder.style.width) >= 0 && parseInt(this._imageNextSlide.style.width) <= 450) {
    this._imageHolder.style.backgroundPosition = "right";
    this._imageHolder.style.width = (parseInt(this._imageHolder.style.width) - 1) + 'px';
    console.log(this._imageNextSlide.style.background);
    this._imageNextSlide.style.width = (parseInt(this._imageNextSlide.style.width) + 1) + 'px';
  } else {
    console.log("reached 0px");
    if (parseInt(this._imageHolder.style.width) == 0) {
      this._imageHolder.style.background = this._imageNextSlide.style.background;
      this._imageHolder.style.width = 450 + 'px';
      this._imageHolder === this._imageNextSlide;
      this._imageHolder.className = "orginalImage";
      this._imageNextSlide.style.width = 0 + "px";
      this._imageNextSlide = this._dummyImageNextSlide;
      this._imagesElement.appendChild(this._imageHolder);
      this._imagesElement.appendChild(this._imageNextSlide);
      clearInterval(this._myTimer);
    }
    clearInterval(this._myTimer);
    clearInterval(this._elementSlideChange);
  }
}

So when the user clicks on the Next arrow button, the event listener for “click” is triggered. This creates a div for the current image on display, and creates a new div, which will contain the next image. The image slide and rotation works correctly (whether it’s onLoad or onClick). The issue I have is if I click the Next button, while the new div image is sliding into position, it causes it to run into an infinite loop, so the same div with the image to be displayed keeps sliding in, and the more you click the Next button, the faster the image starts to rotate.

I have tried putting a clear interval for the image rotation and slider, but I do understand my code is wrong, which causes the infinite loop of the sliding image. And I know I am close to finishing the functionality.

Can anyone please advise where I could be going wrong? Or should I try to implement the sliding DIV in another way?

Once again please don’t recommend jQuery.

And thank you for your help in advance.

Kush

  • 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-16T21:59:24+00:00Added an answer on June 16, 2026 at 9:59 pm

    To solve the issue, I did re-write the entire code, where I had a next and previous button event listener.

    myProject.Utils.addHandler(this._nextImageElement, "click", myProject.Utils.createDelegate(this._changeImage, this));
    

    Both the buttons will call the same function :

    _changeImage: function (e)
    

    In this function I check to see if the function is Transition (changing images),
    I declare a boolean var forward = e.target == this._nextImageElement;

    Then check to see the current index if forward ? Add 1 else minus 1

    this._currentImageIndex += forward ? 1 : -1;
    

    If its at the end of the Array and forward is true, assign the this._currentImageIndex to reset to 0 or Array.length – 1 if it’s in reverse

    Then call another function which gives the ‘div’ a sliding effect. In this case call it this._transitionImage(forward);

    In this function, set the this._inTranstion to true. (Because the div’s are sliding in this case).

    The following code solved the issue i was having.

    this._slideImageElement.style.backgroundImage = "url(\"" + this._imgArray[this._currentImageIndex].src + "\")";
    
    this._slideImageElement.style.backgroundPosition = forward ? "left" : "right";        
    this._slideImageElement.style.left = forward ? "auto" : "0px";
    this._slideImageElement.style.right = forward ? "0px" : "auto";
    

    The above code is very important as the object is to place the “sliding in div” Left or Right of the current Visible “div” to the user, and this is mainly dependent on if the forward variable is true or false.

    var i = 0;
    

    Then start the transition by

    setInterval( function() {
        this._currentImageElement.style.backgroundPosition = (forward ? -1 : 1) * (i + 1) + "px";
           this._slideImageElement.style.width = (i + 1) + "px";
    

    Notice the forward will determine if the bgPosition will go to the left if its forward as we multiple by -1 or +1,
    So for example
    If the user clicks NEXT BUTTON,
    Forward = true
    So the first thing we do is set the

    this._slideImageElement.style.backgroundPosition = "left"
    

    Then

    this._slideImageElement.style.left = "auto"
    this._slideImageElement.style.right = "0px"
    

    This means when the sliding image moves in its background position is LEFT but the div is placed on the RIGHT to 0px;
    then this._currentImageElement.style.backgroundPosition = -1 * (i + 1)
    Which moves the position of the currentImageElement to the left by 1px,

    Increase the width of the slideImage which in this case is right of the current div,
    and as the current div moves to the left the sliding image starts to appear from the right. (By default set the width of slideImageElement to 0px so the div exists but isn’t visible to the user). This gives it the slide effect of moving forward new image coming from the right.

    this._slideImageElement.style.width = (i + 1) + "px";
    

    then declare it to stop when it it’s the image width. In this case it will be 500px.
    if ((i = i + 2) == 500) {
    In this if statement reset the currentImageElement background and the background position “right” or “left” don’t really matter as long it has been reset.

    Clear the interval
    Set the transition to false again
    Then call a setTimeout for the function changeImage, which will continue until the slide is completed.

    The following shows the reset code as this is very important to prevent repeating the same image (This solved my entire issue)

    // set the current image to the "new" current image and reset it's background position
    this._currentImageElement.style.backgroundImage = "url(\"" + this._imgArray[this._currentImageIndex].src + "\")";
    this._currentImageElement.style.backgroundPosition = "right";
    
    // reset the slide image width
    this._slideImageElement.style.width = "0px";
    
    // clear the transition interval and mark as not in transition
    clearInterval(this._transitionInterval);
                    this._inTransition = false;
    
    // setup the next image timer
    this._nextImageTimeout = setTimeout(myProject.Utils.createDelegate(this._changeImage, this), 2500);
    

    }

    I have provided a thorough detail because then it easier to understand the logic of the problem, and even if your not having the same issue, this may help you fingure out any problem.

    I couldn’t provide a JSfiddle, as i have created my CSS using Javascript, there are different ways of doing this, but i wanted to understand the logic behind the forward and reverse, and having a timer which continuously goes forward.

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

Sidebar

Related Questions

Someone please recommend a better title for this question. I'm not sure what to
Can someone please recommend a good CRM system? If this question should not be
Please not that I fully understand this is a dumb ass idea, but its
Please do not flag this as a duplicate, I'm not asking about what is
Please do not mark it as a dupe of this question just yet: Bold
I'm not sure this is the right forum if it's not please feel free
I have this little jQuery plugin: <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd>
I'm building a framework (this is a vast simplification -- please don't recommend using
Can you please recommend a solution how to achieve a format like this with
Please recommend a C++ compression (zip) library for a commercial, closed-source application. So, not

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.