I have this script which will show the content of an external URL but only after the entire site has loaded. How can I make it show the content of the external file faster? Here is the script: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript"> jQuery(function($){ setInterval(function(){ $.get( 'http://www.site.com/file.php', function(newRowCount){ $('#rowcounter').html( newRowCount ); }); },1000); }); </script> Code (JavaScript): I have added the script above in the header part of my site, before the rest of the scripts but that did not fix the problem... Any idea? Thanks!
You don't need to use a setInterval function as jQuery(function($) { is run after the DOM has finished loading. So remove that and that should speed things up for you.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript"> jQuery(function($){ $.get( 'http://www.site.com/file.php', function(newRowCount){ $('#rowcounter').html( newRowCount ); }); }); </script> Code (markup):
Thanks, but from that code is missing the part which will refresh the content of the div #rowcounter.
Ohh right. Well if you want to load dynamic content with jQuery every second then of course it's going to have times when it's slow. You could try to write the code again in native JavaScript: http://kyleschaeffer.com/development/the-perfect-jquery-ajax-request/ which would be faster as it wouldn't rely on the large library. I don't think you are going to be see huge changes thou!