Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2012
    Location
    Eastleigh, UK
    Posts
    25
    Thanks
    5
    Thanked 2 Times in 2 Posts

    My first short script -not working.....

    I'm attempting to replace data on one page with constantly changing data from a second page. With the help of a friend I have put together the script below but without success.

    Code:
    <script>
       $(document).ready(function(){
          var ids = ["item1","item2","item3",];
    
          for (index = 0; index < ids.length; ++index) {
              $( "#"+id[index]).load( "live_data.php #"+id[index]);
          }
    
          setInterval(function(){
          $( "#"+id[index]).load( "live_data.php #"+id[index]);
          },10000);
       });
    </script>
    So on my first page where I want the live data I have "<span id="item1">nothing</span>" and on my "live_data.php" page I have the same span but containing the live data.

    Hopefully I have explained that adequately. Any help gratefully received.

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,865
    Thanks
    6
    Thanked 1,029 Times in 1,002 Posts
    1. You have a stray comma in the array (the last one).
    2. The variable “index” in the setInterval function isn’t defined. I suppose you need a loop in there, too.


    First try a bare-bones example without the interval. Also, check the JS console for any errors. It might tell you what’s wrong.

  • #3
    New Coder
    Join Date
    Sep 2012
    Location
    Eastleigh, UK
    Posts
    25
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Well I did as you advised and found an error. After a while I found it rather tedious adding all the id's I required so I changed the code to work with a class.
    Code:
    <script>
       $(document).ready(function(){
    
          var ids = $('.live_data').map(function() {
              return this.id;
          });
    
          for (index = 0; index < ids.length; ++index) {
              $( "#"+ids[index]).load( "current_table_2.php #"+ids[index]);
          }
       });
    </script>
    The problem I had not thought of was that every time the script gets to a class that needs the data refreshing it loads/searches the whole content of 'current_table_2.php' again rather than just the value. This cycle takes approx 12ms and soon adds up. Is there a better/faster way of achieving this?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •