javascript implements a registration button that can be clicked after 10 seconds

  • 2020-06-12 08:25:42
  • OfStack

This article illustrates how javascript can make the registration button clickable after 10 seconds. Share to everybody for everybody reference. The specific analysis is as follows:

1. The registration button is initially unavailable,disabled
2. Start the timer, setInterval, run the CountDown method once per second, and set a global variable with the initial value of 10.
Count down the global variable in the CountDown method, and then write the count down to the register button (read the protocol carefully (8 seconds left)).
Up to the value of the global variable < =0, make the registration button available, and set the text of the button to "Agree!"


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title></title>
 <script type="text/javascript">
 var MyCount = 10;
 var intervalID;
 function CountDown() {
  var btnReg = document.getElementById("btnReg");
  if (btnReg) {
  // I'm going to add here btnReg The judgment of whether it's empty, 
  // Because the Internet might be slow ,setInterval Later, btnReg The button is not loaded yet 
  if (MyCount <= 0) {
   btnReg.disabled = ""; // or btnReg.disabled="disabled" Can also be 
   btnReg.value = " agree ";
   clearInterval(intervalID); // Clearance timer 
  }
  else {
   btnReg.value = " Please read the agreement carefully ( And then there were " + MyCount + " seconds )";
   MyCount--;
  }
  }
 }
 intervalID=setInterval("CountDown()", 1000);
 </script>
</head>
<body>
 <textarea> Please agree to the agreement of this site </textarea><br />
 <input id="btnReg" type="button" value=" agree " disabled="disabled" />
</body>
</html>

I hope this article has been helpful for your javascript programming.


Related articles: