-4

攻击 = 攻击按钮。

当我运行此代码(单击按钮)时,禁用该按钮大约需要 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++;
    };
}
4

3 回答 3

1

是的,这是因为 1000 毫秒计时器。如果将其设置为 10,则禁用将需要 10 毫秒。如果您需要它开始禁用,请将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++;
    }
}
于 2013-10-21T02:53:24.347 回答
0

将计时器设置为 0

var disabledStartTimer = setInterval(disabledTimer, 0);

或者干脆打电话

disabledTimer();
于 2013-10-21T02:56:01.080 回答
0

设置这个:

var disabledStartTimer = setInterval(disabledTimer, 1000); // 1 sec

对此:

var disabledStartTimer = setInterval(disabledTimer, 0); // 0 sec

因为这里的时间以毫秒为单位。您可以肯定这一点,这确实是因为setInterval.

于 2013-10-21T02:58:14.470 回答