攻击=攻击按钮。
当我运行此代码(单击按钮)时,大约需要1秒钟来禁用该按钮。如何将其设置为立即禁用?我假设它是因为1000毫秒计时器,但我不确定。
var disabledStartTimer = setInterval(disabledTimer, 1000);
var start = 0;
function disabledTimer() {
if (start > 5){
clearInterval(disabledStartTimer);
console.log("disabled timer stopped");
blitz.disabled = false;
}
else {
blitz.disabled = true;
start++;
};
}
答案 0 :(得分:1)
blitz.disabled = true
代码超出间隔回调:
var disabledStartTimer = setInterval(disabledTimer, 1000);
var start = 1; // set to 1 to maintain consistency (i.e. call blitz.disabled = true the same amount of times as the original code.
blitz.disabled = true;
function disabledTimer() {
if (start > 5) {
clearInterval(disabledStartTimer);
console.log("disabled timer stopped");
blitz.disabled = false;
}
else {
blitz.disabled = true;
start++;
}
}
如果你需要做更复杂的事情并且这个代码只是一个例子,你可以将复杂的语句包装在一个函数中并从区间外部和内部调用它:
var disabledStartTimer = setInterval(disabledTimer, 1000);
var start = 1; // set to 1 to maintain consistency (i.e. call blitz.disabled = true the same amount of times as the original code.
function disableBlitz() {
blitz.disabled = true;
}
disableBlitz();
function disabledTimer() {
if (start > 5) {
clearInterval(disabledStartTimer);
console.log("disabled timer stopped");
blitz.disabled = false;
}
else {
disableBlitz();
start++;
}
}
答案 1 :(得分:0)
将计时器设置为0
var disabledStartTimer = setInterval(disabledTimer, 0);
或只是致电
disabledTimer();
答案 2 :(得分:0)
设置:
var disabledStartTimer = setInterval(disabledTimer, 1000); // 1 sec
对此:
var disabledStartTimer = setInterval(disabledTimer, 0); // 0 sec
因为时间以毫秒计算。你可以肯定这一点,这真的是因为setInterval
。