JS implementation of automatic countdown 30 seconds after the button is available

Source: Internet
Author: User


The countdown is often used in web development to limit the user's actions to the form. For example, want users to read the relevant protocol information in a certain period of time to allow users to continue the next step, such as in the collection of mobile phone verification code to allow users after a certain period of time (without receiving the verification code in the case) to obtain the verification code again. So today I'd like to show you how to use JavaScript to implement this simple application.


Scenario 1: When the user registers, read the relevant protocol information before activating the button
Some Web site registration requires users to agree with the so-called user Agreement, such as information, if the content of the agreement is very important, some sites will require the newly registered users must read the relevant protocol information to activate the Next button to submit the form. In order to allow users to read the protocol information (the actual user has not really read I do not know), the developer will design a countdown such as 30 seconds, 30 seconds later, the form submission button will be activated, the following to see how the specific implementation.

<form action= "http://www.111cn.net/" method= "POST" name= "Agree" >

<input type= "Submit" class= "button" va Lue= "Please look carefully < terms and conditions of service and declaration > (m)" Name= "Agreeb" >

</form>


Assuming that there is such a form, the rest of the form is omitted, only a submit button, the initial button is not available, when the 30-second countdown is displayed, the button will show "I agree" and can be clicked to activate.
We use the raw JS to achieve this effect:

<script>  var secs = 30;  document.agree.agreeb.disabled=true;  for (var  i=1;i<=secs;i++)  {      window.settimeout ("Update"  + i +  ")",  i * 1000); }  function update (num)  {      if ( Num == secs)  {          document.agree.agreeb.value  = "  I   with    ";           document.agree.agreeb.disabled=false;      }      else {           var printnr = secs-num;           document.agree.agreeb.value =  "Please look carefully < service terms and statements >  ("  + printnr + ")";      } }  </script>


We set the time to 30 seconds, of course, you can also set the time you want, the button is disabled, that is, not clickable, and then cycle 30 seconds, through the window.settimeout call Update () function, the current seconds and Countdown, if the countdown is completed then show "I Agree", and activate the button.
Application Scenario 2: The user activates the SMS channel to send the authentication code SMS to the user's mobile phone The Ming
Many sites in the authentication of users need to improve the security of user information, which will be binding with the user's mobile phone, so will send the user's mobile phone authentication code information, if the user filled out the correct code to submit a background, then the operation will be successful. and send the verification code may because of various reasons have sent unsuccessful situation, and can not allow users to constantly click to send. So the developer uses the countdown to deal with this kind of problem, the user activates the text message, if after 30 seconds did not receive the authentication code text message can again allow clicks to send the text message.

<form action= "http://www.111cn.net/" method= "post" name= "MyForm" >

<input type= "button" class= "button" V Alue= "Get Cell phone authentication code" name= "Phone" onclick= "Showtime" >

</form>

The above form adds an OnClick event to the button and calls the Showtime () function.

<script>  function showtime (t) {      document.myform.phone.disabled= true;      for (i=1;i<=t;i++)  {           window.settimeout ("Update_p" (" + i + ", "+t+") ",  i * 1000);       }   }    function update_p (num,t)  {       if (num == t)  {          document.myform.phone.value  = "  Resend  ";          document.myform.phone.disabled =false;      }      else {           printnr = t-num;           document.myform.phone.value =  "  ("  + printnr + ") seconds later resend";      } }  </script> 


, as in Scenario 1, when the button status is disabled after the button is clicked, the Update_p () display the countdown by calling Window.settimeout, and when the countdown is complete, the button displays "Resend", when the button status is available.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.