Generate multiple random numbers in JavaScript -
i'm trying write script roulette system , want script run until bank variable reaches 0 or 11,000, , produce 3 pieces of data after each spin.
i have left parts of code out simplicity. code in if else statement not problem. running script until variable reaches point i'm stuck.
would able me rewrite script please? 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 + " neither high nor low.</p>"); // removed } else if ( number > 18 ) { document.write("<p>the number " + number + " high number.</p>"); // removed } else { document.write("<p>the number " + number + " low number.</p>"); // removed } }; spin(); document.write("<p>total bank " + bank + ".</p>"); document.write("<p>the next bet " + bet + ".</p>"); })();
if you're calling loop within page (i.e. don't want page hang duration, or want display results) should use requestanimationframe:
window.requestanimframe = (function(){ return window.requestanimationframe || window.webkitrequestanimationframe || window.mozrequestanimationframe || function( callback ){ window.settimeout(callback, 1000 / 60); }; })(); then set loop call if bank within accepted range:
function spinloop() { spin(); //perform ui updates here if (bank >= 0 && bank <= 11000) requestanimframe(spinloop); } you will, of course, need call function start loop:
spinloop();
Comments
Post a Comment