views:

241

answers:

4

My CMS project has a stylish web 2.0 login screen that fades over the screen using javascript. How come that even though I have made 120% sure that images are preloaded (I used the resource monitor in development tools) they still take a second to show up when my login screen appears. It completely destroys the fanciness! Take a look:

http://www.dahwan.info/Melior

When you click login, the screen is supposed to fade to dark using a 75% alpha 1px png image. Even though the image is preloaded, it doesn't show up until after the animation is done. However, if you click cancel and log in again, the animation flows smoothly and perfectly.

Can anyone think of a solution to this problem? I'm having it with the rest of my CMS' GUI as well. It's like there was no image preloading what so ever.

Thanks for answers

EDIT: Ah yes, I'm currently developing this CMS for Google Chrome 5.0.375.99, adding multi browser compatibility later. Sorry for leaving that out

A: 

The UI thread can only manage one task at a time -- it will either execute javascript or update the UI. If there is a lot of javascript parsing/executing before the code that preloads the image, that might be causing the delay.

Another suggestion is to disable clickability on the login link until after the image has been detected on the page.

To do so is fairly straightforward:
function disableBtn(el){
var btn = document.getElementById(el);
var btnId = btn.id;
btn.disabled = true;
}

To re-enable, set btn.disabled = false (after detecting that your image has been added to the DOM).

frabjousB
I have excluded images being loaded slowly as a cause to my problem, seeing as in some cases I have been examining the resource monitor for several minutes, and even seeing the images in the monitor, before clicking the login button. But thanks for your reply
Codemonkey
+1  A: 

I have come up with a workaround to my problem. I have now tested this in three browsers on two different computers with perfect results. In short, in my image preloading function in javascript, i added each of the images into the DOM in an invisible Div tag.

$('#img_preload').append($('<img />').attr('src', img.src));

The images now being added to the Dom at page load, and according to my theory resting in the memory of my low-end computers, they appears instantly when my CMS needs them.

Thanks for your comments :)

Codemonkey
A: 

You could also just preload them into an array. Your problem might be caused by what is known as "garbage collection". This is where the browser looks objects for that are consuming memory who no longer have an instance on the screen, and are not being referenced by anything else in memory.

If you preload images into your web age, they should be loaded into the cache, though. So, they should still re-appear when referenced again. However, images can also disappear if the cache expiration is not set long enough for these types of files.

Your problem could be browser-specific.... I have found that it is always best to create an "anchor" for pre-loaded content by placing them into an image array and then using the array to call up the images when needed instead of the image URL(URI).

Here is a quick-and-dirty article that covers this topic.

http://articles.techrepublic.com.com/5100-10878_11-5214317.html

exoboy
Loading the images into an array is the method I was using already. I apologize again for supplying so little information. Anyway, I've solved the problem and replied with it :)
Codemonkey
Cool! I am glad it works well. But, the way you are doing it will actually take up space on the stage(page), whereas mine will keep it in the ethereal plane (memory) without any risk of a physical presence on the stage or chance of displacing content and it does not require any CSS to be defined.
exoboy
+1  A: 

A useful information about this problem:

The Codemonkey solution works because, by putting the images in a hidden div, the browser have to keep those images in memory and ready for a possible change of div's visibility. If the user needs to change de visibility of div from hidden to block, it has to be done instantly. This is why just load all images into an array doesn't work properly.

Jr. Hames
Well using an array to store image ojects has worked for me for years. So, please try to refrain from making comments that you are not 100% certain of....
exoboy