0

I'm trying to write a script for roulette system and I want the script to run until the bank variable reaches 0 or 11,000, and produce three pieces of data after each spin.

I have left parts of the code out for simplicity. The code in the if else statement is not the problem. Running the script until the variable reaches a certain point is where I'm stuck.

Would anyone be able to help me rewrite this script please? Thanks in advance.

(function() {
  var bank = 10000;
  var bet = 1;

  function spin() {
    var number = Math.floor(Math.random() * 36);

    if ( number == 0 ) {
      document.write("<p>The number " + number + " is neither high nor low.</p>");
      // removed
    } 
    else if ( number > 18 ) {
      document.write("<p>The number " + number + " is a high number.</p>");
      // removed
    }
    else {
      document.write("<p>The number " + number + " is a low number.</p>");
      // removed
    }
  };

  spin();

  document.write("<p>Total bank is now " + bank + ".</p>");
  document.write("<p>The next bet is " + bet + ".</p>");

})();
4

1 回答 1

0

如果您在页面内调用此循环(即您不希望页面在此期间挂起,或者想要显示结果),您应该使用requestAnimationFrame

window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       ||
          window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame    ||
          function( callback ){
            window.setTimeout(callback, 1000 / 60);
          };
})();

然后将您的循环设置为仅在银行在您接受的范围内时才调用自己:

function spinLoop() {
    spin();

    //perform your UI updates here

    if (bank >= 0 && bank <= 11000) requestAnimFrame(spinLoop);
}

当然,您首先需要调用此函数来启动循环:

spinLoop();
于 2013-04-05T20:40:04.673 回答