简体   繁体   中英

How to clear JavaScript timeout using the if else statement if condition is true

I tried using some JavaScript timing with an if statement. The script executed but I set another if statement that clears the timeout if the condition is true. It's not working.

Code:

var timeing = setTimeout("timelet()");
if(hour==true && min==true){
    clearTimeout(timeing);
    return true;
}

How do I clear the timeout if the condition is true?

It is hard to say what you are trying to do, but either this;

// set the time out and then check and clear it every time its run
function timelet() {
    // some stuff

    if(hour==true && min==true){
        clearTimeout(timeing);
    }
}
var timeing = setTimeout(timelet(), 1000);

OR

// don't set the time out if not required
if(hour==true && min==true) {
    // do something
} else {
    var timeing = setTimeout(timelet(), 1000);
}

Your problem is definitely here:

if(hour==true && min==true)

Try to figure out this combined statement becomes true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM