简体   繁体   English

如何在 JavaScript 中显示一段时间的文本?

[英]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!"我正在使用JavaScript制作游戏..所以,我想显示“你赢了!” text for 5 seconds.But when I execute it, It just shows the text for very less time and disappears.文本 5 秒。但是当我执行它时,它只显示文本的时间很短,然后消失了。

  {
      //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 是球员的名字,CUP 就像是 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.这段代码是让Sofia在触到Cup后回到初始位置。

So, basically I want to display "You Win!"所以,基本上我想显示“你赢了!” for some time (Say... 5 seconds).一段时间(比如... 5 秒)。

You will display the message and after 5 seconds delete it.您将显示该消息并在 5 秒后将其删除。

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

setTimeout(function(){
    document.getElementById("idOfBlock").innerHTML = '';
}, 5000);

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

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