How to disable a button with a timed loop?

陌路散爱 提交于 2020-01-22 02:50:07

问题


i'm trying to disable a button for 6 seconds while in a loop but so far I can't quite figure it out.

var disabledStartTimer = setInterval(disabledTimer, 1000);

function disabledTimer() {
    var start = 0;
    if (start > 6) {
        clearInterval(disabledStartTimer);
        console.log("disabled timer stopped");
        attack.disabled = true;
    } else {
        attack.disabled = false;
        start++;
    };
}

attack = the button I click to attack.


回答1:


var start = 0;
if (start > 6){

Clearly this will always go into the else. You set the variable to 0 and then test if it's greater than 6... it isn't. You likely wanted this to be a global, move it outside of the function.



来源:https://stackoverflow.com/questions/19485298/how-to-disable-a-button-with-a-timed-loop

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