Solved

Polling in JQuery.. must be a better way?

Posted on 2011-09-21
3
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36575764
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
ID: 36577135
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
ID: 36577647
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

631 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