JavaScript: Exemplo de uso do clearInterval

From AdonaiMedrado.Pro.Br
Jump to: navigation, search
<html>
  <head>
    <title>Minha Página</title>
  </head>  
  <body>
    <input type="text" id="txtMeuContador" />
    <input type="button" id="btOK" value="OK" onclick="javascript:f2();" />
    <script language="javascript">
      var i = 0;
      var intervalId;
      function f1()
      {
	document.getElementById("txtMeuContador").value = i++;
      }
      function f2()
      {
	clearInterval(intervalId);
      }
      intervalId = setInterval(f1, 500);
    </script>
  </body>
</html>