按钮显示倒计时JS

test.html

<script type="text/javascript" src="test.js"></script>

<input type="button" id="fontId" value="send sms" onclick="clickBtn();" />

 

test.js

var totalTime = 5 * 1000;//总时间
var remainingTime = totalTime;//初始化剩余时间

function clickBtn(){
    //设置触发事件的时间间隔
    var interval = setInterval(function(){
        myEvent();
    },1000);
    
    //设置多长时间后触发事件。
    setTimeout(function(){
        remainingTime = totalTime;//初始化剩余时间
        document.getElementById("fontId").value = "send sms";
        clearTimeout(interval);//清除或关闭括号里的任务对象。
    },totalTime);
}

//我的事件
function myEvent(){
    remainingTime = remainingTime - 1000;
    var remainingSecond = remainingTime / 1000;
    
    document.getElementById("fontId").value = remainingSecond;
}

 

posted @ 2022-07-25 15:19  新手娃娃菜  阅读(313)  评论(0编辑  收藏  举报