1. In this example, if the Div block movement continues to click the button, the speed will be more and more quickly, so the other timer to open the time to turn off the previous timer. Here the timer defines the position will affect whether the timer is closed, if the timer is defined as a local variable, then every time the click is null, then the clearinterval does not play a role in the shutdown timer. Therefore, the timer must be defined as a global variable.
2. For speed this variable, no, he put it outside, directly as a local variable.
<!DOCTYPE HTML><HTML><HeadLang= "en"> <MetaCharSet= "UTF-8"> <title></title> <style>#div1{width:200px;Height:300px;background:#ffccdd;position:Absolute; } </style></Head><Body><inputID= "BTN"type= "button"/><DivID= "Div1"></Div><Script> varobtn=document.getElementById ('btn'); varOdiv=document.getElementById ('Div1'); varTimer= NULL; Obtn.onclick= function(){//var timer = null;clearinterval (timer); Timer=SetInterval (function (){ var Speed= 1; if(Odiv.offsetleft> -) {clearinterval (timer); } Else{oDiv.style.left=Odiv.offsetleft+ Speed+'px'; } }, -); };</Script></Body></HTML>
JS Motion Basics 1