Using setTimeout to add cooldown time to a button

那年仲夏 提交于 2019-12-12 02:53:00

问题


I try to create an object method called coolDown, when a button is clicked, it will disable the button and unlock it afer cooling time is over.

But when I test it, it will disable the button, but, did not unlock the button after the cooling time is over, why is it?

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;
}

Appreciate for your answer..


回答1:


it should be

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

Boolean("false"); is evaluated to true



来源:https://stackoverflow.com/questions/36594962/using-settimeout-to-add-cooldown-time-to-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!