jquery - Javascript alert box shows up before executing previous statement -
i having strange issue, not surprising bit of javascript newbie. creating simple high-low card game. (draw 2 cards, highest card wins). anyways, code below.
the basic flow of program pretty simple. choose 2 random numbers (1-52). these numbers mapped corresponding card. (i.e. number 1 ace of spades, number 37 jack of clubs, etc.). anyways, after drawing cards, program display corresponding card , determine winner. @ end of of this, have alert comes , and tells winner of draw , asks if user wants play again.
the problem having this: though program should have displayed image of card , output results text area, alert box shows before of occurs , never displays cards or results. ideas? posting of code far , appreciated. in advance.
function drawcards() { var oppcard = randnumber(); var customercard = randnumber(); while (oppcard == customercard) { customercard = randnumber(); } var oppcardname = displaycard(oppcard, "oppimage"); var customercardname = displaycard(customercard, "custimage"); var result2 = "your card was: " + customercardname; var result1 = "the opponent's card was: " + oppcardname; var result3 = determinewinner(oppcard, customercard); var result4 = result3 + '\n' + result1 + '\n' + result2; $("#textareares").text(result4); playagain(result3); } function determinewinner(oppscard, customerscard) { var oppvalue = oppscard % 13; var customervalue = oppscard % 13; var winnerstring = ""; if (oppvalue == 0) { oppvalue = 13; } if (customervalue == 0) { customervalue = 13; } if (oppvalue == customervalue) { winnerstring = "you tied."; } else if (oppvalue > customervalue) { winnerstring = "you lose."; } else if (oppvalue < customervalue) { winnerstring = "you win!!"; } return winnerstring; } function randnumber() { var min = 1; var max = 52; var random = math.floor(math.random() * (max - min + 1)) + min; return random; } function playagain(resultstring) { if (resultstring == "you lose." || resultstring == "you win!!") { alert(resultstring); var conf = confirm("play again?"); if (conf == true) { $("#textareares").text(""); document.getelementbyid("custimage").src="./cardimages/default.png"; document.getelementbyid("oppimage").src="./cardimages/default.png"; } else { window.location = "#mainmenupage"; } } else { alert(resultstring); alert("try again."); $("#textareares").text(""); document.getelementbyid("custimage").src="./cardimages/default.png"; document.getelementbyid("oppimage").src="./cardimages/default.png"; } }
so did not place code in here display card function, because testing exceptionally long. giant switch case 52 random numbers. finished product pulling xml file, used testing purposes. (if, reason, need see display cards function, let me know , can post it.) anyway, recap, last call made in drawcards() function playagain function. upon running code results nor card images displayed. jumps straight alert called playagain function. pretty noobish question, little perplexed it. guys can offer appreciated. thanks.
[edit: performs correctly in computer's browser. however, problem happens on mobile device phone or tablet. doing incorrectly here. appreciated.]
changes in browser doesn't show long javascript code running.
the browser event driven, changing element in dom doesn't show change immediately, instead event triggered redraw element. when function has finished running, browser handle pending events , show changes.
so, when building application, have use same approach browser has chance show changes.
Comments
Post a Comment