View Full Version : preloading

12-19-2002, 09:51 PM
I have a bunch of images with the names 20.gif through 2f.gif (following hex). I want them to all be preloaded in as short amount of script as possible. There's It's a nice trick to store variables in hex, but if you increment it through the FOR loop, or any other type of arithmetic, it converts it to base10. This isn't good. I had something more like this in mind:

for (i=N; i<0x30; i++)
"p"+i=new Image()

Of course I could use Arrays, etc, but in the long run, it wouldn't be much shorter than 16 lines of preload script. Loading time is afterall a priority when preloading. What's a good preloading script for this?

12-19-2002, 10:11 PM
0x20 or whatever is a perfectly legit Number literal, so just use that. All you need is the toString() method, which takes a radix for an argument (10 is default).
var hex, arrImgs = new Array();
for (i=0x20, j=0x30; i<j; i++) {
hex = i.toString(16);
arrImgs["p"+hex] = new Image();
arrImgs["p"+hex].src = hex + ".gif";

12-19-2002, 11:38 PM
Radix? I don't know what that is...But you mean it is that easy to convert a number to hex?

12-20-2002, 02:11 AM
The radix is the base of the number system. For binary, the base is 2, octal 8, decimal (our favorite) 10, and hex is 16.

Javascript interprets the 0x20 as it's base 10 value, which in this case is 32. Applying [B]toString(16) gives us the hex value, again in this case, 20.

Yes, it's that easy :D