javascript - JQuery Script looping error -


<html> <head> <script src="jquery.js"></script> <script> $(document).ready(function(){     setinterval(function(){     var = 1;         while(i<3){               var left = $("#"+i).offset().left;             $("#"+i).css({'left':left}).animate({'left':'-10000px'},8000);             if(i == 1){                 i++;             }             if(i == 2){                 i--;             }         }       },2500);  }); </script> </head>  <body> <div id=mydivwrapper style="overflow:hidden"> <div id=1 style="right:0;width:100%;height:100%;background:url('1.jpg');position:absolute;"></div> <div id=2 style="right:0;width:100%;height:100%;background:url('2.jpg');"> </div> <body> </html> 

i'm trying create jquery image slider using while loop. here integer i'm incrementing. don't want 3 loop stop. therefor used decision statement finding if 1 increment , if 2 decrement it. causes lag on browser , doesn't work. know script crazy. can figure out made mistake?

your problem here..

if(i == 1){    i++; } if(i == 2){     i--; } 

see when 1, first statement becomes true , second if going executed @ time 2 again decrements value of i. problem causes lag on browser. use else or else if solve this.

here is.

if(i == 1) {     i++; } else {     i--; } 

or

if(i == 1) {    i++; } else if(i==2) {   i--; } 

both same , work you.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -