zoukankan      html  css  js  c++  java
  • 超实用的JavaScript代码段 Item4 --发送短信验证码

    发送短信验证码

    实现点击“发送验证码”按钮后,按钮依次显示为“59秒后重试”、“58秒后重试”…直至倒计时至0秒时再恢复显示为“发送验证码”。在倒计时期间按钮为禁用状态 .

    第一步、获取按钮、绑定事件、设置定时器变量和计时变量

    第二步、添加定时器,每隔1秒钟计时减 1,直至当计时小于等于 0 时清除定时器,按钮恢复为“发送验证码”,否则显示为“X秒后重试”

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <script type="text/javascript">
        window.onload=function(){
            var send=document.getElementById('send'),
                times=60,
                timer=null;
            send.onclick=function(){
              // 计时开始
              var that = this;
                this.disabled=true;
                timer = setInterval(function(){
                    times --;
                    that.value = times + "秒后重试";
                    if(times <= 0){
                        that.disabled =false;
                        that.value = "发送验证码";
                        clearInterval(timer);
                        times = 60;
                    }
                    //console.log(times);
                },1000);    
            }    
        } 
        </script>
    </head>
    <body>
        <input type="button" id="send" value="发送验证码">
    </body>
    </html>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    设置按钮是否为禁用时,send.disabled=true; send.disabled=false;
    true和false不能加引号!true和false不能加引号!true和false不能加引号!否则值永远为真。 
    也可用send.setAttribute('disabled','disabled');
    或send.removeAttribute('disabled');
    • 1
    • 2
    • 3
    • 4

    这里写图片描述

    转载:http://blog.csdn.net/i10630226/article/details/49531083

  • 相关阅读:
    【python+selenium】selenium grid(分布式)
    【python】导入自定义模块
    Maven的配置以及IDEA导入本地Maven
    java历史概述
    JVM 内存调优 与 实际案例
    ConcurrentHashMap实现线程安全的原理
    Request.UrlReferrer详解
    等比例缩放生成缩略图
    JavaEE的ajax入门
    javaee三层架构案例--简单学生管理系统
  • 原文地址:https://www.cnblogs.com/huangyin1213/p/5855772.html
Copyright © 2011-2022 走看看