1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Run this script faster

Discussion in 'jQuery' started by Fracisc, Aug 2, 2014.

  1. #1
    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!
     
    Fracisc, Aug 2, 2014 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    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.
     
    HuggyStudios, Aug 4, 2014 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    No, it's not. Not unless he places it inside the $(document).ready() - function.
     
    PoPSiCLe, Aug 4, 2014 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4
    $(function() {
    });
    Is the same as $(document).ready()
     
    HuggyStudios, Aug 4, 2014 IP
  5. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #5
    Could you please give me the exact code to test it?
     
    Fracisc, Aug 4, 2014 IP
  6. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #6
    
    <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):
     
    HuggyStudios, Aug 5, 2014 IP
  7. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #7
    Thanks, but from that code is missing the part which will refresh the content of the div #rowcounter.
     
    Fracisc, Aug 5, 2014 IP
  8. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #8
    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!
     
    HuggyStudios, Aug 5, 2014 IP
  9. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #9
    I am not good at these.. maybe I'll use iframe instead..
     
    Fracisc, Aug 5, 2014 IP