簡體   English   中英

如果出現“ if”后如何在自身內部退出javascript函數?

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

我正在創建游戲,但在功能中遇到了計數器問題。 我希望函數循環五次,然后如果我的元素“ nocolnum”的值為5,則需要函數退出或中斷。 這是我的HTML:

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

這是我的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 (nocolnum == 5) {
    return true;
}

要么

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM