Hacker News new | past | comments | ask | show | jobs | submit login

We do something like this:

  $(function(){
      setTimeout(function(){
          /* load twitter, facebook */
      }, 2000);
  });
This tries to make sure that all the essential work to be done on the page is completed before the less important Twitter, FB buttons are loaded.



You are making part of your page always load at least 2 seconds slower than is probably necessary.

$(function(){}); is shortcut for $(document).ready(); which waits until all other external content has been loaded. [1]

If for some reason you are loading something else you need after .ready() fires, and you can't load that inside of a single .ready() call for some reason, you can potentially add multiple callbacks to .ready() (they will be executed in order [2]), or attach a .load() to that particular item instead (maybe even inside your .ready().)

Timeout hacks are not a performance improvement.

[1] http://api.jquery.com/ready/ [2] http://stackoverflow.com/questions/5263385/jquery-multiple-d...


$(document).ready does "not wait until all other external content has been loaded". It fires when the DOM is fully parsed, irrespective of whether there are still images or iframes or other assets still loading.

http://stackoverflow.com/questions/4395780/difference-bw-onl...


I think he was getting at the idea that in general each page has some specific-to-the-page JavaScript work to do as soon as it's ready. The delay punts initialization of the less-important plugins, allowing time for the important stuff in a page-generic way. Your point that the delay is a messy way of accomplishing this is certainly valid.


The onload event fires after the page has fully loaded - all scripts, image files, etc. I would say that it is more effective than your snippet for waiting until "essential work" has been finished.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: