带有“prompt”的“while”循环从不显示任何“console.log”
`while` loop with `prompt` is never showing any `console.log`
我只是想检查我的控制台是否连接到我的代码,以前它是为同一个程序连接的,但现在它甚至没有加载仅包含标题的基本 HTML 页面,并且它没有在控制台中显示任何内容。为什么会这样?
var todos = [ "whats up dude!!" ];
var input = prompt("what would you like to do?");
while (input !== "quit") {
if (input === "list") {
todos.forEach(function(todo, i) {
console.log(i + ": " + todo);
});
}
else if (input === "new") {
var newTodo = prompt("what do you want?");
todos.push(newTodo);
}
else if (input === "delete") {
var index = prompt("Enter index of todo to delete");
todos.splice(index, 1);
console.log("Todo Removed");
}
input = prompt("what would you like to do?");
}
console.log("You have Quit!!");
<!DOCTYPE html>
<html>
<head>
<title>one more try</title>
<script type="text/javascript" src="tryy.js"></script>
</head>
<body>
<h1>its the last resort</h1>
<h4>hope i win this!!</h4>
</body>
</html>
您可以在使用 setTimeout
:
的每个提示之前放置一个小的延迟,而不是从不让出对脚本或页面的控制的紧密循环
var todos=["whats up dude!!"];
function interactWithToDos()
{
var input=prompt("what would you like to do?");
if(input==="list")
{
todos.forEach(function(todo, i)
{
console.log(i +": "+ todo);
});
}
else if(input==="new")
{
var newTodo=prompt("what do you want?");
todos.push(newTodo);
}
else if(input === "delete")
{
var index = prompt("Enter index of todo to delete");
todos.splice(index, 1);
console.log("Todo Removed");
}
if(input !== "quit")
{
setTimeout(interactWithToDos, 0);
}
else
{
console.log("You have Quit!!");
}
}
setTimeout(interactWithToDos, 0);
我只是想检查我的控制台是否连接到我的代码,以前它是为同一个程序连接的,但现在它甚至没有加载仅包含标题的基本 HTML 页面,并且它没有在控制台中显示任何内容。为什么会这样?
var todos = [ "whats up dude!!" ];
var input = prompt("what would you like to do?");
while (input !== "quit") {
if (input === "list") {
todos.forEach(function(todo, i) {
console.log(i + ": " + todo);
});
}
else if (input === "new") {
var newTodo = prompt("what do you want?");
todos.push(newTodo);
}
else if (input === "delete") {
var index = prompt("Enter index of todo to delete");
todos.splice(index, 1);
console.log("Todo Removed");
}
input = prompt("what would you like to do?");
}
console.log("You have Quit!!");
<!DOCTYPE html>
<html>
<head>
<title>one more try</title>
<script type="text/javascript" src="tryy.js"></script>
</head>
<body>
<h1>its the last resort</h1>
<h4>hope i win this!!</h4>
</body>
</html>
您可以在使用 setTimeout
:
var todos=["whats up dude!!"];
function interactWithToDos()
{
var input=prompt("what would you like to do?");
if(input==="list")
{
todos.forEach(function(todo, i)
{
console.log(i +": "+ todo);
});
}
else if(input==="new")
{
var newTodo=prompt("what do you want?");
todos.push(newTodo);
}
else if(input === "delete")
{
var index = prompt("Enter index of todo to delete");
todos.splice(index, 1);
console.log("Todo Removed");
}
if(input !== "quit")
{
setTimeout(interactWithToDos, 0);
}
else
{
console.log("You have Quit!!");
}
}
setTimeout(interactWithToDos, 0);