[英]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.
我希望函数循环五次,然后如果我的元素“ nocolnum”的值为5,则需要函数退出或中断。 here is my html:
这是我的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: 这是我的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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.