I am writing a bicycle, the cube should move to the right endlessly and return. Only 1 iteration occurs. If you add left_go () to else if {setInterval (right_go, 5);}, everything works, but over time the animation starts to slow down and hang. Help me please?
var animate_right = setInterval(right_go, 5); function right_go () { var left_padding = cube_style.left; if (left_padding!=width_square){ cube.style.left = x +"px"; x++; console.log(left_padding); } else if(left_padding=width_square){ clearInterval(animate_right); var animate_left = setInterval(left_go, 5); } } function left_go (animate_left) { var left_padding = cube_style.left; if (left_padding!='-1px'){ cube.style.left = x +"px"; x--; console.log(left_padding); } else if(left_padding!=width_square){ clearInterval(animate_left); } }
setIntervalusesetInterval, but to use recursivesetTimeout- Aliaksandr Pitkevich