JavaScript 发送短信案例

通过javascript 实现 发送短信验证码的案例


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>title>
head>
<body>
<input type="text"> <button id="btn">点击发送短信button>
body>
html>
<script>
    var btn = document.getElementById("btn")
        var  count = 3;
        var timer = null;
        btn.onclick = function() {
            this.disabled = true; //this是指向btn的
            var that = this;//把btn对象给that
            timer = setInterval(sendTextMessage,1000);
            function sendTextMessage() {
                count--;
               if(count >= 0){
                   that.innerHTML = "还剩余"+count+"秒";
               }
                else {
                   that.innerHTML = "重新发送";
                   that.disabled = false;
                   clearInterval(timer); //清除定时器
                   count = 5 ;
               }
            }
        }
script>

你可能感兴趣的:(JavaScript)