title: JavaScript Resource Management Design Pattern
body: <p>As a web developer, a common problem I find myself tackling is waiting for something to load before doing something else. In particular, I often hide (using either <code>display: none;</code> or <code>visibility: hidden;</code> depending on the situation) elements while waiting for a background image or a CSS file to load.</p>
  
  <p>Consider <a href="http://www.last.fm/music/Radiohead/In+Rainbows" rel="nofollow noreferrer">this example</a> from Last.FM. They overlay a semi-transparant PNG over each album art image so that it looks like it's inside a jewel-case. They let it load when it loads, so depending on your internet speed, you may see the art image by itself (without the overlay) temporarily. In this case, the album art looks fine without the jewel-case effect. But in similar situations, I have found that I don't want the user to see the site's design mangled as resources incrementally load. So, in rare cases I have hidden everything from the user until the whole kit and kaboodle has loaded. But this is often a pain to write out, and may force the user to wait for a pretty long time to see anything (besides "loading..." text).</p>
  
  <p>I can think of (and have used on occasion) some obvious solutions/compromises:</p>
  
  <ol>
  <li><p>Use some inline CSS so that as certain parts of the DOM load and render, they will immediately have the correct size/position/etc.</p></li>
  <li><p>Immediately render the navigation part of the site, so that if the user wanted to use the current page purely to get somewhere else, they don't have to wait for the rest to load.</p></li>
  <li><p>Load pixelated images first as placeholders for layout while lazy-loading higher quality images as replacements.</p></li>
  <li><p>Something quirky like using a cute animated gif to distract the user during a "loading..." phase.</p></li>
  <li><p>Show useful information as a reference while loading the full UI. (Something akin to <a href="http://gmailblog.blogspot.com/2009/05/new-in-labs-inbox-preview.html" rel="nofollow noreferrer">Gmail Inbox Preview</a>, etc.)</p></li>
  </ol>
  
  <p>(Sorry if my question was basically just asked and answered...)</p>
  
  <p>Despite all of these ideas, I still find myself hoping there are better ways of doing some of these things. So I guess what I'm looking for is some inspiration and/or any creative ways of dealing with this problem that you guys may have seen out in the wild.</p>