简体   繁体   中英

How to exit a javascript function inside itself after an “if”?

I´m creating a game and I've got stuck with a counter in a function. I want the function to loop five times and then if my element "nocolnum" has a value of 5, i need the function to exit or break. here is my html:

<!DOCTYPE html>
<html>
    <head>
        <title>title</title>
    </head>
    <body>
        <span id="opt1"></span>
        <span id="nocolnum">0</span>
    </body>
</html>

here is my js:

function func(num) {
    num = num + 1;
    var opt = document.getElementById('opt1');
    opt.innerHTML= num + "%" ;
    var move = setTimeout("func("+num+")",15);
    var nocolnum = document.getElementById('nocolnum');

    if(num == 100){
        nocolnum.innerHTML++;
        clearTimeout(move);
    }

    if (nocolnum == 5) {
        // I dont know what to put here
        // to break out
        // a break, return or something??
    }

    var one = 0;
    func(one);
}
if (nocolnum == 5) {
 // I dont know what to put here
 // to break out
 // a break, return or something??
 return false;

}

If you want to break function you can simlpy return

if (nocolnum == 5) {
    return true;
}

or

if (nocolnum == 5) {
    return false;
}

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