获取短信验证码倒计时代码实现

时间:2022-02-08 22:03:59

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="WM.MemberRegister.Web.Demo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>无标题页</title>     <script type="text/javascript">

  var clock = '';

  var nums = 10;

  var btn;

  function sendCode(thisBtn)

  { 

   btn = thisBtn;

   btn.disabled = true; //将按钮置为不可点击

   btn.value = nums+'秒后可重新获取';

   clock = setInterval(doLoop, 1000); //一秒执行一次

  }

  function doLoop()

  {

   nums--;

   if(nums > 0){

    btn.value = nums+'秒后可重新获取';

   }else{

    clearInterval(clock); //清除js定时器

    btn.disabled = false;

    btn.value = '点击发送验证码';

    nums = 10; //重置时间

   }

  }

</script> </head> <body>     <form id="form1" runat="server">     <div>     <input type="button" style="height:32px;width:120px;" value="点击发送验证码" onclick="sendCode(this)" />     </div>     </form> </body> </html>