为什么甚至在打印console.log输出之前会出现2-3个提示?

时间:2019-03-12 00:36:29

标签: javascript

let yourName = prompt("Enter name");

console.log(`Just a test ${yourName}`);
console.log("Welcome to the test. Please enter your username: ");

let username = prompt("Enter username");

当我运行此javascript代码时,同时有2个提示,然后输出console.log。如何使它起作用,所以首先提示出现yourName,然后在console中打印console.log行,然后在几秒钟后要求输入用户名?

还是有什么方法可以接受控制台本身而不是提示的输入?

2 个答案:

答案 0 :(得分:1)

您可以将$timeout与以下示例一起使用:

angular.module('app', []).controller('ctrl', function($scope, $timeout) {

  function timeoutFn() {
    $timeout(function() {
      let username = prompt("Enter username");
      console.log(`Your username is: ${username}`);
    }, 1500);
  }

  let yourName = prompt("Enter name");
  console.log(`Just a test ${yourName}`);
  console.log("Welcome to the test. Please enter your username: ");
  timeoutFn();

})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js">
</script>

<div ng-app='app' ng-controller='ctrl'>
</div>

答案 1 :(得分:0)

    var name = prompt("Enter name:");
    console.log('Test name: '+name);
    console.log("Welcome to the test.");
    var userName = prompt('Enter username:');
    console.log('Test username: '+userName);

function myPrompt() {
  var username = prompt('Enter username:')
  console.log('Test username: '+username);
}
    window.setTimeout(myPrompt, 3000);

这是东西吗?