[英]Javascript - Boolean global variable in child function not changing
I am working on s atudent project: a 'hot or cold' game app but can't seem to understand something. 我正在研究一个出色的项目:一个“热还是冷”的游戏应用,但似乎听不懂。
The problem is that when there is a correct guess, the flag I have setup to register the guess does not register the change (caused by the correct guess) permanently and the user can continue guessing. 问题是,当有正确的猜测时,我设置为注册猜测的标志不会永久注册更改(由正确的猜测引起),并且用户可以继续猜测。
I change the value of the global variable winflag when the user makes the correct guess but for some reason it goes back to false (ie no guess yet) and continues the game. 当用户做出正确的猜测时,我更改了全局变量winflag的值,但由于某种原因,它又返回了false(即尚未猜测)并继续游戏。
Can anyone please point out why is this happening? 任何人都可以指出为什么会这样吗?
//INSIDE DOCUMENT.READY
//global variable declaration
var winflag=false;
//event handler for click on guess button
$('#guessButton').click(function(){
event.preventDefault();
if(!winflag) {
console.log("calling guessfunction")
guessfunction();
}
else {
alert("Game is over");
}
});
//END OF DOCUMENT READY
var guessfunction = function () {
//guesscount++ -- write guesscount in #count
guesscount++;
console.log("guesscount is " + guesscount);
//current guess cguess=get the input from the text box
var tcguess = $('#userGuess').val();
var cguess = +tcguess;
//if cguess>100 - alert "not valid input"
if (cguess>100) {
return alert("Not a valid input");
}
else {
//append cguess in #guessList
$('#guessList').append('<li>'+tcguess+'</li>');
//calculatefeedback(solution,cguess)
calculatefeedback(solution,cguess);
console.log("calculated feedback");
}}
var calculatefeedback = function(sol,guess) {
//difference=absolute value(solution-guess)
var difference = Math.abs(solution-guess);
//if difference=>70 --- return you're freezing
if (difference >= 70) {
winflag=false;
return alert("You're freezing!");
}
//else if difference=>30 --- return youre cold
else if (difference >= 30) {
winflag=false;
return alert("You're cold!");
}
//else if difference=>15 --- return youre warm
else if (difference >= 15) {
winflag=false;
return alert("You're warm!");
}
//else if difference=>5 --- return hot
else if (difference >= 5) {
winflag=false;
return alert("You're hot!");
}
//else if difference>=1 --- return burning
else if (difference >= 1) {
winflag=false;
return alert("You're burning!");
}
//else if difference=0 --- return GUESSED
else if (difference == 0) {
winflag=true;
return alert("You guessed!");
}
//else alert "not valid input"
else {
return alert("Not a valid input");
}
//end game
}
If you are setting var winflag=false;
如果您设置var winflag=false;
inside a $(document).ready(function() {})
then it is not being set on the global scope. 在$(document).ready(function() {})
,则不会在全局范围内设置它。 The winflag
is being set within the scope of the document ready. 将在准备好的文档范围内设置winflag
。 You need to set it like so window.winflag=false
. 您需要像window.winflag=false
这样进行设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.