计时器

//<editor-fold desc="倒计时事件  调用countDown(id,显示的文本)">
var time = 5;//倒计时的时间
var showTimeId = "";//显示倒计时的位置
var showTimeText = "";//倒计时结束的文本

async function countDown(id, text) {

    if (time >= 5) {
        showTimeId = id;
        showTimeText = text;
        startTime();
    }else {
        console.log("stop!")
    }
}

function startTime() {
    time = time - 1;
    $("#" + showTimeId).html(time + "秒");
    if (time == 0) {
        $("#" + showTimeId).html(showTimeText);
        time = 5;
        clearTimeout();
        return;
    }
    console.log(time);
    setTimeout('startTime()', 1000);
}

//</editor-fold>
原文地址:https://www.cnblogs.com/caitangbutian/p/11696090.html