I have a spritesheet that is 1320x1320, where each image is 120x120, for a total of 120 pictures. I figured that if I did this in JS:
for (var i = 1; i < 114; i++) {
Heroes[i].style.backgroundPosition = -72 * i + "px " + Math.floor(i / 11) * -72 + "px";
// i'm assigning more values here that aren't in the overall class here but didn't want to bore you guys with pointless code
}
I figured by writing this in JS it would pretty much do the CSS but take extra resources for doing all the math and inputting it to CSS, so I went in Excel and made it do all the math for me and copy pasted 120 CSS classes, then I created an array with the names and made JS link them to the class names instead. Shouldn't it load faster? If true, I doubt I saved massive resources, but I like trying to code as clean and efficient as possible.
Now something that just came to me and why I made this question is, would it actually be even faster to go back to JavaScript, but with a timer (say 10ms) after the page has loaded? So the initial load time loads as fast as possible, players can see my menu screen for the game I'm working on, and by the time they use the menu to take them somewhere, it will have done the JavaScript to load and place all my images. Since computers are so insanely fast, loading even 500 images with JS after the initial page loads the spritesheets + html and css, should be nothing, right?
Any advice and information on this topic is greatly appreciated! Thanks in advance for any help.
EDIT:
So I tested it quite a bit and found one solution that seems to work perfectly fine, the timeout 10ms thing:
setTimeout(function() {
var heroesJS = document.createElement("script");
heroesJS.src = "heroes.js";
document.getElementsByTagName("head")[0].appendChild(heroesJS);
},3000);
First question, I copied this from w3schools, but I thought everyone said to put scripts before the body tag ends, so is it fine to use the head tag there? Also now my main new question is:
Do you think the above method is better or should I just wrap my for loops that create the images and placement for everything inside a function so it doesn't actually do any 'heavylifting' but loads the JS file and once everythings done loading, I then call the function to do the work behind the scenes?