var oTil=document.getElementById("title") var i=20; 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=i+'px'; i-- }},3 ) if(i>300){clearInterval(timer)} }