控制台未加载

时间:2019-06-14 14:06:54

标签: javascript html google-chrome-devtools

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

我只是想检查我的控制台是否已连接到代码,并且以前是否为同一程序进行过连接,但是现在它甚至没有加载基本的html页面,该页面仅包含标题,并且在我执行此操作后显示了要控制台的任何工作代码。为什么会这样???? image

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>

1 个答案:

答案 0 :(得分:1)

您可以在每次提示前使用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);