Question about prompt () in Js

0

How could I do:

 var name = prompt("Ingresa tu nombre");
document.write("Bienvenido " + name );
var question = prompt("¿Te animas a jugar?");

First, the prompt is executed, after the welcome message is displayed in the document and again the prompt is executed.

    
asked by user889 25.04.2018 в 08:43
source

2 answers

2

You can try to put a delay (a delay) between the welcome message and the second prompt, for example 1 second or whatever you want.

Try this:

var name = prompt("Ingresa tu nombre");
document.write("Bienvenido " + name );
setTimeout(function(){ var question = prompt("¿Te animas a jugar?"); }, 1000);

After writing the message on the screen, it will wait 1 second and skip the other prompt.

SAVE RESPONSE FROM THE SECOND PROMPT

var name = prompt("Ingresa tu nombre");
document.write("Bienvenido " + name + "<br/>");
setTimeout(function(){
    var question = prompt("¿Te animas a jugar?");
    document.write("El usuario "+name+" " + question + " se anima a jugar.");

}, 1000);
    
answered by 25.04.2018 / 08:52
source
1

You could choose an option that solves two problems in one:

  • Validates that empty data is not written in prompt . This validation could be extensive, by means of some function, for example, controlling only numerical values or with a certain extension, etc.
  • Writes you the information in the order, because you write it after validating.

Let's see an example in which we only validate that the prompt is not empty:

var strName=prompt( "Escribe tu nombre","" );
while (!strName) {
   strName=prompt( "Escribe tu nombre","" );
}

document.write( "Bienvenido " + strName + "<br />");

var strQuestion = prompt( strName + " ¿Te animas a jugar?", "" );
while (!strQuestion) {
   strQuestion = prompt( strName + "¿Te animas a jugar?", "" );
}

document.write( strName + ", tu respuesta ha sido: "+ strQuestion );
    
answered by 25.04.2018 в 11:36