Skip to content
Advertisement

How to display text for some amount of time in JavaScript?

I am making a game using JavaScript.. So, I want to display a “You Win!” text for 5 seconds.But when I execute it, It just shows the text for very less time and disappears.

  {
      //If Sofia touches the CUP then there should be a timeout for n seconds   
    textSize(30);
    text("YOU WIN!!!", 200, 100);
    sofia.x = 380;
    sofia.y = 375;
  }

Sofia is the Player’s name and the CUP is just like a finish line for Sofia.

    sofia.x = 380;
    sofia.y = 375;

This piece of code is to put Sofia back to the initial position after it touches the Cup.

So, basically I want to display “You Win!” for some time (Say… 5 seconds).

Advertisement

Answer

You will display the message and after 5 seconds delete it.

document.getElementById("idOfBlock").innerText = msg;

setTimeout(function(){
    document.getElementById("idOfBlock").innerText = '';
}, 5000);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement