K.Z
K.Z

Reputation: 5075

setInterval function call only once in program

I am trying to make simplest image slide, which scroll image in div from left to right.

my issue is setInterval function runs only once and I am struggling of why or how to fix.

Many thanks in advance. I am calling setInterval function jQuery document.ready(function..

<div class="highlight_Block" id="FeaturesList_block">
   <div id="img_wrapper_05">
      <ul id="FeaturesList_Ul">

       <li id="DEMO1_000001" class="FeaturesList_li">
          <img src="http://www.hostpaperz.com/wp-content/uploads/2013/06/abstract-color-349935.jpg" height="214" width="321">
       </li>

       <li id="DEMO1_000002" class="FeaturesList_li">
          <img src="http://www.hostpaperz.com/wp-content/uploads/2013/06/Abstract-06-HD-wallpaper.jpg" width="321">
       </li>

       <li id="DEMO1_000003" class="FeaturesList_li">
          <img src="http://www.hostpaperz.com/wp-content/uploads/2013/06/abstract-wallpapers-wallpaper-array-wallwuzz-hd-wallpaper-2830.jpg" height="214" width="321">
       </li>

      </ul>
   </div>
</div>

setInterval(function () {
  var xyPosition_05 = $("#img_wrapper_05").position();
  var next_X_Position = xyPosition_05.left + 321;
  next_X_Position = '-' + next_X_Position + 'px'
  $("#img_wrapper_05").animate({ left: next_X_Position }, 1000);
},5000);

CSS:

  .highlight_Block {
   float:left;
   width:321px;
   height:300px;
   margin-left:15px;
   margin-top:15px;
   background-color:aliceblue;

  }

  #FeaturesList_Ul {
  list-style-type:none;
  margin:0px;
  padding:0px;
 }


.FeaturesList_li{
 display:inline-block;
 }


 #img_wrapper_05 {
 width:auto;
 height:214px;
 position:absolute;
 background-color:green;
 }

Upvotes: 1

Views: 1197

Answers (3)

user1741851
user1741851

Reputation:

There is no reason to believe that the setInterval is not working. If your images are not sliding, it may be because of the wrong CSS.

Step 1: Add an alert to see if it is entering into the setInterval function.

alert("setInterval Entered");

Step 2: If you are getting the alerts every 5 seconds, it's not a javascript issue. Try this

var next_X_Position = xyPosition_05.left - 321;
next_X_Position = next_X_Position + 'px';

After first try, the value of left property is already negative.

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388326

in the second iteration the left value get two --

setInterval(function () {
    var xyPosition_05 = $("#img_wrapper_05").position();

    var next_X_Position = xyPosition_05.left + 321;

    next_X_Position = (next_X_Position<0? next_X_Position : -next_X_Position) + 'px'


    $("#img_wrapper_05").animate({ left: next_X_Position }, 1000);

},5000);

Demo: Fiddle

Upvotes: 5

Neha Shah
Neha Shah

Reputation: 19

After animation just set div to its default position using following code:

 $("#img_wrapper_05").css('property','value');

thats it! :)

Upvotes: 0

Related Questions