javascript - Images don't get displayed before a pop-up box -
i started learning javascript doing simple card game , i'm stucked @ problem. want show 4 cards image before user can select trump popup box. but, everytime run code cards images displayed after popup box , not before. please have @ relevant code:
function preloadimages() { var imgs = []; (var = 0; < max_length_deck; i++) { imgs[i] = new image(); imgs[i].src = 'img/' + deck[i] + '.png'; } } function generatedeck() { (i = 0; < colour.length; i++) { (x = 0; x < number.length; x++) { deck.push(colour[i] + '' + number[x]); } } } function shufflecards() { cards.length = 0; (i = 0; < max_length_deck; i++) { var random = math.floor(math.random() * deck.length); cards.push(deck[random]); deck.splice(random, 1); } } function dealcards() { generatedeck(); preloadimages(); shufflecards(); (var = 0; < 4; i++) { window.document.images[i].src = 'img/' + cards[i] + '.png'; //i defined 4 image tags @ html file } selecttrump(); } function selecttrump() { var result = false; while (result != true) { trump = prompt("please enter trump:", ""); result = checktrump(trump); } }
i searched , tried several things (jquery load handlers; window.settimeout), nothing worked , don't problem. so, thank hint!
br kjaer
in function dealcards() have wait images loaded first, before dealing cards. can use add eventhandler this:
<html> <head> <script> function loadimage() { alert("image loaded"); } </script> </head> <body> <img src="w3javascript.gif" onload="loadimage()" id="myimage"> </body> </html> //or use: // example function addevent(element, evnt, funct){ if (element.attachevent) return element.attachevent('on'+evnt, funct); else return element.addeventlistener(evnt, funct, false); } addevent( document.getelementbyid('myimage'), 'load', function () { alert('image loaded!'); } );
Comments
Post a Comment