var x = 0, back = true, cicle = setInterval(function(){ x += back ? 0.1 : -0.1; if (x>1||x<0) back = !back; console.log(x); },100);