I’m working on a JS program with my students that will print to the console, but I need to print to the console immediately after inputs from prompts instead of waiting until all code has executed. For example, in the code below I want to print firstName as soon as it is input, but nothing gets printed to the console until after both prompts. Is what I want possible on repl.it?
let firstName = prompt('What is your first name?');
console.log(firstName);
let lastName = prompt('What is your last name?');
console.log(lastName);
console.log() and prompt() are made for two different interfaces, so they don’t work well together. However, I think you need to add a delay to it like this:
let firstName = prompt('What is your first name?');
console.log(firstName);
// Force a 10 millisecond delay before running the rest of the code.
setTimeout(function(){
let lastName = prompt('What is your last name?');
console.log(lastName);
}, 10);
I actually got it working the way I want it to. In the normal repl window nothing prints to the console until the game is finished. But if I open it in a new tab it prints to the console after each prompt input.
Hi @JJMichael I forked your repl to test this out and I see what you mean. When using the Replit UI the console doesn’t update until the program has finished running.