使用setTimeout为按钮添加冷却时间

Using setTimeout to add cooldown time to a button

本文关键字:冷却 时间 添加 按钮 setTimeout 使用      更新时间:2023-09-26

我尝试创建一个名为coolDown的对象方法,当单击某个按钮时,它将禁用该按钮,并在冷却时间结束后解锁它。

但当我测试它时,它会禁用按钮,但是,冷却时间结束后没有解锁按钮,为什么?

var btncd = new test();
function test() {
}
test.prototype.coolDown = function() {
    document.getElementById("cd").disabled = "true";
    setTimeout(function() {document.getElementById("cd").disabled = "false";}, 2000);
}
window.onload = function() {
    document.getElementById("cd").onclick = btncd.coolDown;
}

感谢你的回答。。

它应该是

document.getElementById("cd").disabled = false; // without ""

Boolean("false");被评估为true