Problem Loading Dynamic Gallery

I tried that, no luck.
When the issue occurs, where the gallery is not populated correctly, if you refresh the page manualy the gallery gets the correct images.

By code you mean running a query on the images database for that specific car ref, and then pass the results to the gallery?