最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript - How to WaitSetTimeOutSleepDelay - Stack Overflow

programmeradmin6浏览0评论

This again is my Rock Paper Scissors game.

At present state the user can't see what's happening because after being prompted for input(Rock, Paper or Scissors) they are immediately reprompted.

The question is how can I make the program delay such that they at least can read what's going on.

I've read that sleep() does not exist in JavaScript. I'm trying to use setTimeOut however, the setTimeOut is causing the program to not run.

Any ideas on how I can delay the next user input after the first user input. This can be done via any JS solution.

This is my present code

function playUntil(rounds) {
        var playerWins = 0;
        var computerWins = 0;
        setTimeout(function() {
        while ((playerWins < rounds) && (computerWins < rounds)) {
          var computerMove = getComputerMove();
          var winner = getWinner(playerMove, computerMove);
          console.log('The player has chosen ' + playerMove + '. The computer has chosen ' + computerMove);
          if (winner === "Player") {
              playerWins += 1; 
          } 
          else if (winner === "Computer") {
              computerWins += 1;
          } 
          if ((playerWins == rounds) || (computerWins == rounds)) {
              console.log("The game is over! The " + winner + " has taken out the game!");
              console.log("The final score was Player - [" + playerWins + "] to Computer - [" + computerWins + "]");
          }
          else {
              console.log(winner + ' takes the round. It is now ' + playerWins + ' to ' + computerWins);
          }
          }
      return [playerWins, computerWins]
    ;},5000);
    }

This again is my Rock Paper Scissors game.

At present state the user can't see what's happening because after being prompted for input(Rock, Paper or Scissors) they are immediately reprompted.

The question is how can I make the program delay such that they at least can read what's going on.

I've read that sleep() does not exist in JavaScript. I'm trying to use setTimeOut however, the setTimeOut is causing the program to not run.

Any ideas on how I can delay the next user input after the first user input. This can be done via any JS solution.

This is my present code

function playUntil(rounds) {
        var playerWins = 0;
        var computerWins = 0;
        setTimeout(function() {
        while ((playerWins < rounds) && (computerWins < rounds)) {
          var computerMove = getComputerMove();
          var winner = getWinner(playerMove, computerMove);
          console.log('The player has chosen ' + playerMove + '. The computer has chosen ' + computerMove);
          if (winner === "Player") {
              playerWins += 1; 
          } 
          else if (winner === "Computer") {
              computerWins += 1;
          } 
          if ((playerWins == rounds) || (computerWins == rounds)) {
              console.log("The game is over! The " + winner + " has taken out the game!");
              console.log("The final score was Player - [" + playerWins + "] to Computer - [" + computerWins + "]");
          }
          else {
              console.log(winner + ' takes the round. It is now ' + playerWins + ' to ' + computerWins);
          }
          }
      return [playerWins, computerWins]
    ;},5000);
    }
Share Improve this question edited Feb 22, 2016 at 2:07 Ash Pettit asked Feb 22, 2016 at 0:43 Ash PettitAsh Pettit 4572 gold badges6 silver badges12 bronze badges 7
  • 1 "I'm trying to use setTimeOut however, it's throwing an error"... and that error is? – Phil Commented Feb 22, 2016 at 0:45
  • The code you've shown doesn't even use setTimeout. – Bergi Commented Feb 22, 2016 at 0:47
  • 6 If your inputs to a tic tac toe game are rock, paper and scissors, then you have much bigger problems than this. – JK. Commented Feb 22, 2016 at 0:47
  • 1 Guys don't vote down a question so quickly. Now it's simply not going to get answered and it's a very valid question – Ash Pettit Commented Feb 22, 2016 at 0:55
  • 1 Thankyou for the first non-troll answer @RobG. Looking into this now – Ash Pettit Commented Feb 22, 2016 at 0:58
 |  Show 2 more comments

2 Answers 2

Reset to default 25

You cannot return a value for a parent function on a setTimeout, setInterval or another child function because have different scopes.

You can use promises instead: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Bad:

function x () {
  setTimeout(function () {
     return "anything";
  }, 5000);
}

Using promises:

function x () {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      resolve("anything");
    }, 5000);
  });
}

Then you can call function like:

x()
.then(
  function (result) {
    alert(result); // Do anything.
  }
);

PD: I have bad English, I'm sorry!.

This took me a long time to figure out but here was the solution.

Create this function

function sleep(miliseconds) {
    var currentTime = new Date().getTime();
    while (currentTime + miliseconds >= new Date().getTime()) {
    }
}

Add this into my code where I wanted the delay

sleep(3000)
发布评论

评论列表(0)

  1. 暂无评论