Search code examples
javascriptprompt

Javascript delaying prompts


Let's say I have 5 functions (it doesn't matter what they do). In order to make the last function work, I need the user to input something, so I decide to include the following line:

var irrelevant = prompt("Question that you don't need to know")

The other four functions just log stuff to the console. However, what's happening (and it doesn't really matter but, for stylistic reasons it's annoying me) is that the prompt command in the fifth and final function leaps up before any of the other functions have had a chance to log their info to the console. Is there any way to delay the prompt command for just a moment or two so it doesn't do that?


Solution

  • I had a similar problem with some of my own code not too long ago. @mplungjan is pointing you down the right track. you could try putting your prompt into the following -

    setTimeout(function(){
        var irrelevant = prompt("Question that you don't need to know")
    }, 0);
    

    This will add a 0 millisecond delay to your prompt but it can be enough to keep the prompt at bay until that line is reached.

    Hope this helps.