简体   繁体   中英

Why does 2-3 prompts appear before even printing console.log output?

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

when i run this javascript code, there are 2 prompts at the same time and then the console.log is printed. How do I make it work so first prompt yourName appears and then console.log lines are printed in console and then it asks for username after a few seconds?

or is there any way to accept input in Console itself instead of prompts?

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

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

Is this something?

You can use $timeout with below sample:

 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> 

You can use the window.setTimeout method to execute a function after the timer expires. The following code will display the username prompt after 2000 ms.

 var username; function usernamePrompt() { username = prompt('Enter username:') console.log(`username: ${username}`); } let yourName = prompt("Enter name") if (yourName) { console.log(`Just a test ${yourName}`); console.log("Welcome to the test. Please enter your username: "); window.setTimeout(usernamePrompt, 2000); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM