gpt4 book ai didi

javascript - `while` 循环与 `prompt` 从不显示任何 `console.log`

转载 作者:行者123 更新时间:2023-11-30 19:29:17 29 4
gpt4 key购买 nike

我只是想检查我的控制台是否连接到我的代码,以前它是为同一个程序做的,但现在它甚至没有加载仅包含标题的基本 HTML 页面,并且它没有显示任何内容安慰。为什么会这样?

Screenshot showing a prompt dialog box, but no console output and the HTML page not loaded.

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);

关于javascript - `while` 循环与 `prompt` 从不显示任何 `console.log`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56599841/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com