问题
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