Mohammad Istanboli Mohammad Istanboli - 1 year ago 58
jQuery Question

Jquery setTimeout is not working properly in loop

hello I have a page that have three divs inside of it looks something like thisenter image description here

each div has 500px height what am trying to do is making the screen auto scroll each three seconds till reach the next div position and once its reach the last div go back and start from the beginning and do this for infinite here is my j query code

$(document).ready(function(){
myfunction();
});

num = 0;
function myfunction(){
if(num == 1500)
{
num = 0;
}

setTimeout(function(){$('html, body').animate({scrollTop:num}, "normal")},3000);
num = num + 500;
myfunction();
}

Answer Source
setTimeout(function(){
  $('html,    body').animate({scrollTop:num}, "normal");  
  num = num + 500;
  myfunction(); 
}
,3000);