简体   繁体   中英

Javascript - changing image won't work unless i alert()

I'm building a JavaScript based poker game that get some updates from a server via Ajax.

when player are in "show down" each one of them supposed to show what card he holds, I'm changing the pictures of each showdown hand (from blank card to an actual card ie King of spades img)

I have the weirdest problem: when i change the images (as shown in the code below), the images wont change from blanks to actual cards, they remain "blank.png". whats weird is if i remove the "//" from line9 (and get an alert message) the cards are shown with their actual image ie "Ace of spades.png".. what's with that?!

function executeShowDown(){
    (...)
var playerCard1Id = "#player"+(playerNum)+"card1"; //the specific image id for hole card #1
var playerCard2Id = "#player"+(playerNum)+"card2"; //the specific image id for hole card #2
var card1 = "res/images/cards/"+handArr[0]+".png";
var card2 = "res/images/cards/"+handArr[1]+".png";
$(playerCard1Id).attr("src", card1);   
$(playerCard2Id).attr("src", card2); 
$(playerCard1Id).css('visibility','visible');
$(playerCard2Id).css('visibility','visible');
//alert("endShowDown");         #@#@# LINE 9 #@#@#
    return;
}

This might be happening too fast. Maybe add something to make it wait for 1 second or so, or you could use a callback function, I see you use return there so maybe the other function where you call this from does not wait for it to end.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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