javascript - 在调用函数之前更新 inner.html
问题描述
如果用户输入的值小于 5,我希望更新 inner.html 并再次提示用户。该代码本身就可以运行,但我希望它进行 inner.html 更新并再次提示用户,而不是仅在没有更改 inner.html 的情况下进行提示。
编辑:我不认为这是重复的,另一个问题询问回流操作何时以及如何发生。但这并没有解决我遇到的问题,即在提示之前更新inner.html,除非我错过了什么。
players = []
function numberOfPlayers() {
playerCount = prompt("Please enter number of players. A minimum of 5 players is required.", "5");
if (playerCount === null) {
return;
}
if (parseInt(playerCount) >= 5) {
document.getElementById("intro").innerHTML =
"There are " + playerCount + " players.";
namesOfPlayers(playerCount);
} else {
document.getElementById("intro").innerHTML =
"Invalid. A minimum of 5 players is required. Hit the button to try again."
numberOfPlayers(); //I want it to update the text before calling numberOfPlayers() again
//the prompt pops up without the text changing.
}
}
function namesOfPlayers(playerCount) {
for (i = 1; i <= playerCount; i++) {
var playerName = prompt("Enter name of player " + i, "John Doe")
players.push(playerName)
}
document.getElementById("names").innerHTML =
"The players are " + players.toString();
}
</script>
<p id="intro">Click the button to begin the game.</p>
<button onclick="numberOfPlayers()">Begin</button>
<p id="names"></p>
解决方案
推荐阅读
- azure-devops - azure build pipeline does not start as triggered
- google-chrome-devtools - Remove/hide accessibility info from tooltip in Chrome developer tool
- postgresql - 在 Postgres 中展开行
- html - How to remove vertical space while using display: inline with same tag or division?
- azure-logic-apps - 上一个任务失败时无法在 Azure 存储中创建文件:状态代码为“200”的操作的架构定义无效
- jsp - 通过变量索引访问哈希元素
- c# - How to add a textbox row inside the datagrid wpf
- docker - 在 windows 的 jenkins 中配置“Docker Host Uri”时连接被拒绝。怎么解决?
- google-sheets - Zapier: Update google spread sheet row
- scala - 在需要时提取期权价值的优雅方式