0
点赞
收藏
分享

微信扫一扫

发送短信倒计时案例

alonwang 2022-04-04 阅读 32
javascript
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>

<body>
    手机号码:<input type="number" /><button>发送</button>
    <script>
        var btn = document.querySelector("button");
        var num = 5;//定义剩下的秒数
        btn.addEventListener("click", function() {
            btn.disabled = true;
            var timer = setInterval(function() {
                if (num == 0) {
                 //清除定时器和复原定时器
                    clearInterval(timer);
                    btn.disabled = false;
                    btn.innerHTML = "发送";
                    num = 3; //不要忘记还要从原来的num数重新开始
                } else {
                    btn.innerHTML = "还剩" + num + "秒";
                    num--;
                }
            }, 1000);
        });
    </script>
</body>

</html>

 

举报

相关推荐

0 条评论