简体   繁体   English

当我在 Javascript 中将变量设置为 0 时,页面不加载

[英]Page doesn't load when I set variable to 0 in Javascript

 var randomNumber = Math.floor(Math.random() * 6); var attempts = 0 var maximum = 3 while (attempts < maximum){ document.getElementById("submit").onclick = function() { attempts += 1; if (document.getElementById("input").value == randomNumber) { alert("You chose the right number"); } else { alert("WRONG Number. You have " + attempts + " attempts;"); } } }
 <input type="text" id="input"> <button id="submit">Submit</button>

while learning how to use a random number generator, I wantd to add a while loop and give 3 attempts to choose from a random number.在学习如何使用随机数生成器时,我想添加一个 while 循环并尝试 3 次从随机数中进行选择。 When I create a variable to set the number of attempts, if I make the variable equal to 0, my page doesn't load, but if I leave the variable empty without a value, the script doesn't run.当我创建一个变量来设置尝试次数时,如果我使变量等于 0,我的页面不会加载,但如果我将变量留空而没有值,则脚本不会运行。

Any ideas of why this may be happening?关于为什么会发生这种情况的任何想法?

 var randomNumber = Math.floor(Math.random() * 6); var x = 0 var maximum = 3 while (x < maximum){ document.getElementById("submit").onclick = function() { attempts += 1; if (document.getElementById("input").value == randomNumber) { alert("You chose the right number"); } else { alert("WRONG Number. You have " + attempts + " attempts;"); } } }
 <input type="text" id="input"> <button id="submit">Submit</button>

Your while loop runs synchronously, during pageload.您的while循环在页面加载期间同步运行。 The script never finishes, because the while loop's condition is never negated, so control is never yielded back to the browser to repaint the page, so the user never has a chance to input a number or click on the button.脚本永远不会结束,因为while循环的条件永远不会被否定,所以控制权永远不会交还给浏览器以重新绘制页面,因此用户永远没有机会输入数字或单击按钮。

While you could keep using while by promisifying the onclick and await ing a Promise, it'd be better to create the handler outside, once, and have it check if the attempts are maxed out.虽然您可以通过承诺 onclick 并await Promise 来继续使用while ,但最好在外部创建处理程序一次,并让它检查尝试是否已达到最大值。

You also need to use consistent variable names: either use x or attempts , not both:您还需要使用一致的变量名称:使用xattempts ,而不是两者:

 var randomNumber = Math.floor(Math.random() * 6); var attempts = 0 var maximum = 3 document.getElementById("submit").onclick = function() { if (attempts >= maximum) return; attempts += 1; if (document.getElementById("input").value == randomNumber) { alert("You chose the right number"); } else { alert("WRONG Number. You have " + attempts + " attempts;"); } }
 <input type="text" id="input"> <button id="submit">Submit</button>

You could disable the button after the maximum number of attempts has been reached, like:您可以在达到最大尝试次数后禁用该按钮,例如:

 var randomNumber = Math.floor(Math.random() * 6); var attempts = 0; var maximum = 3; document.getElementById("submit").onclick = function(e) { attempts += 1; if (document.getElementById("input").value == randomNumber) { alert("You chose the right number"); } else { alert("WRONG Number. You have " + attempts + " attempts;"). } if (attempts === maximum) e.target;disabled = true; };
 <input type="text" id="input" /> <button id="submit">Submit</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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