Skip to content
Advertisement

Why sometimes does the img jump in this Blackjack game

In this Blackjack game (https://schaulcode.github.io/blackjack/) written in javaScript sometimes when the cards get dealt the img jumps. it looks like the code gets executed faster the the browser can paint the picture. Interestingly this only happens online, during development when the whole program was locally on my machine this issue didn’t occur.

Could anyone explain the reason behind it and what is the best way to solve the problem?

here is a link to the source code on github: https://github.com/schaulcode/blackjack/blob/master/js/script.js

the function responsible for turning the card is called turnCard() and its called by the function moveCard()

here is the relevant code :

JavaScript

Advertisement

Answer

As I pointed out in the question, this issue only happend once the application was online but during development on the local machine it never happened. So it seemed that the browser couldn’t download the image so fast every time a new card was played.

To fix that problem I added this piece of code in the <head>

JavaScript

Now I Preload all images straight when the page loads and there is no need anymore for the browser to download each image individually, speeding up the display time when the card is dealt. And therefore there is no jump int the images anymore.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement