简体   繁体   English

如何实时显示 Javascript 输出?

[英]How can I display a Javascript output in real time?

Apologies if this is a simple question, but is there a way to output Javascript to the console in real-time, while pausing at each user interaction.抱歉,如果这是一个简单的问题,但有没有办法实时将 Javascript 输出到控制台,同时在每次用户交互时暂停。 In other words, if I did something as simple as...换句话说,如果我做了一些像...

 console.log("What's your name: "); let userName = prompt("What is your name?"); console.log(userName); console.log("\\n"); console.log("Nice to meet you " + userName + "! What is your age?"); let userAge = prompt("What is your age?"); console.log(userAge);

...can I get the name outputted to the screen before the age prompt comes up? ...我可以在年龄提示出现之前将名字输出到屏幕上吗? This is doable in other teaching environments (MS Basic & the command prompt), but either doesn't exist in Javascript, or I'm missing something.这在其他教学环境(MS Basic 和命令提示符)中是可行的,但在 Javascript 中不存在,或者我遗漏了一些东西。

Thanks for helping the newb!感谢您帮助新手!

You can make use of setTimeout, something like this:您可以使用 setTimeout,如下所示:

console.log("What's your name: ");
let userName = prompt("What is your name?");
console.log(userName);

console.log("\n");
setTimeout(function(){
console.log("Nice to meet you " + userName + "! What is your age?");
let userAge = prompt("What is your age?");
console.log(userAge);
},1000)

Note: This is one way to do it there are ample of other ways to do it.注意:这是一种方法,还有很多其他方法可以做到。

In case if you want to use promises:如果您想使用承诺:

let userName,userAge, userSport;
function basic(){
console.log("What's your name: ");
var promise1 = new Promise(function(resolve, reject){
userName = prompt("What is your name?");
resolve(userName)
})
promise1.then(function(value){
console.log(value);
console.log("\n");
console.log("Nice to meet you " + value + "! What is your age?");
var promise2 =  new Promise(function(resolve, reject){
userAge = prompt("What is your age?");
resolve(userAge)
})
promise2.then(function(value){
console.log(value)
console.log("Nice to meet you " + userName + " aged: " +userAge + ". Let us know your favorite sports")

var promise3 = new Promise(function(resolve, reject){
userSport = prompt("What is your favorite sports?");
resolve(userSport)
})
promise3.then(function(value){
console.log(value)
console.log("Nice to meet you " + userName + " aged:  " +userAge + " and your favorite sports is " +value)
console.log("\n");
console.log("Thanks");
})
})
})

}
basic()

You can use the async/await functionality and promises.您可以使用async/await功能和承诺。 I personally do not always like to use .then so this is my version of using Promises.我个人并不总是喜欢使用.then所以这是我使用 Promises 的版本。

 const Q1 = () => new Promise(resolve => { let userName = prompt('What is your name?'); console.log(`What is your name: ${userName}`); resolve(userName); }) const Q2 = (userName) => new Promise(resolve => { console.log(`Nice to meet you ${userName}! What is your age?`); let age = prompt('What is your age?'); console.log(age); resolve(age); }) const userPrompt = async () => { let name = await Q1(); let age = await Q2(name); // assigned to variable should you want to use it in next question } userPrompt();

You can now just add additional questions as functions.您现在可以将其他问题添加为函数。

Also here is a link to a JSFiddle .这里还有一个指向JSFiddle的链接。 If you run it and open the console you will see that the name is displayed before the age prompt is opened.如果您运行它并打开控制台,您将看到在打开年龄提示之前显示名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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