Solved

Polling in JQuery.. must be a better way?

Posted on 2011-09-21
3
263 Views
Last Modified: 2012-05-12
Hi,
I'm using Jquery to update a couple divs on a timer and all works well. My problem is in order to accomplish it I have to run separate php files to query the database for each div so it returns the proper data to display for that div.

Is there a way I can simply use one query, pass back all the data and have it populate the appropriate divs? My code I'm using is below:
 
<script>
 $(document).ready(function() {
 	 $("#contest_status").load("application/modules/account/scripts/contest_status.php?account_id=4");
 	 $("#contest_title").load("application/modules/account/scripts/contest_title.php?account_id=4");


   var refreshId = setInterval(function() {
      $("#contest_status").load('application/modules/account/scripts/contest_status.php?account_id=4&randval='+ Math.random());
      $("#contest_title").load('application/modules/account/scripts/contest_title.php?account_id=4&randval='+ Math.random());

   }, 6000);
   $.ajaxSetup({ cache: false });
});
</script>

Open in new window


Thanks for any help.
0
Comment
Question by:tjyoung
  • 2
3 Comments
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
Yes, see this get method
http://api.jquery.com/jQuery.get/

in the success handler, you can parse data and place it in appropriate div
0
 
LVL 1

Author Comment

by:tjyoung
Comment Utility
Great, that looks like the right direction.
Can you give me an idea what the code would look like with a timer?
Not particularly good at this.
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
Comment Utility
function getData()
{
  //ajax call
  $.get('ajax/test.html', function(data) {
     //loading data in various divs
    $('.result').html(data);
    //alert('Load was performed.');
    setTimeout("getData()", 5*1000); //calling this method again after 5 seconds
  })
}
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now