Cosi ancora meglio, se clicchi nuovamente nel bottone si ferma (inoltre ho definito l'oggetto fuori dalla funzione, altrimenti si ripeteva inutilmente):
codice:<html> <head> <script type="text/javascript"> window.onload=function(){ var oggetto=document.getElementById("mioBottone"); oggetto.addEventListener("click",cambia); var timer1; var x=0; var y=0; var intervallo; function cambia(){ x=x+50; y=y+50; this.style.fontSize="40"; this.style.backgroundColor="cyan"; this.style.borderColor="blue"; this.style.color="red"; this.style.position="absolute"; this.style.top =y; this.style.left =x; this.removeEventListener("click",cambia); this.addEventListener("click",ferma); intervallo=setInterval(sposta,1000); } function sposta(){ x=x+50; y=y+50; oggetto.style.top =y; oggetto.style.left =x; } function ferma(){ clearInterval(intervallo); } } </script> </head> <body> <input type="button" id="mioBottone" onclick="" value="cliccami"> </body> </html>

Rispondi quotando