var oTil=document.getElementById("title") var i=20; var j=300 var timer=null; oTil.onclick=function(){ var _this=this; timer=setInterval(function(){ if(i<300){ _this.style.left=i+'px'; i++} else{ _this.style.left=j+'px'; j-- }},3 ) if(i>300){clearInterval(timer)} }