Js button delay operation when mobile phone sends verification code

  • 2020-03-30 03:24:05
  • OfStack

Instance code record:


<script type="text/javascript">
  function start_sms_button(obj){
    var count = 1 ;
    var sum = 30;
    var i = setInterval(function(){
      if(count > 10){
        obj.attr('disabled',false);
        obj.val('Send verification code');
        clearInterval(i);
      }else{
        obj.val(' The remaining '+parseInt(sum - count)+' seconds ');
      }
      count++;
    },1000);
  }
 
  $(function(){
    //Send verification code
    $('#send_sms').click(function(){
      var phone_obj = $('input[name="phone"]');
      var send_obj = $('input#send_sms');
      var val = phone_obj.val();
      if(val){
        if(IsMobile(val)){
          send_obj.attr('disabled',"disabled");
          //Restart the send button after 30 seconds
          start_sms_button(send_obj);
          $.ajax({
            url:'{#url_reset("index/sms")#}',
            data:{'mobile':val},
            dataType:'json',
            type:'post',
            beforeSend:function(){
              show_loading_body();
            },
            complete:function(){
              show_loading_body();
            },
            success:function(data){
              if(data.status!=undefined && (data.status == 'ok' || data.status == 'error')){
                showMsg(data.msg);
              }
            }
          });
        }else{
          showMsg(" Wrong format of mobile phone number ");
        }
      }else{
        showMsg(' The phone number cannot be empty ');
      }
    });
  });
</script>


Related articles: