Solved

Polling in JQuery.. must be a better way?

Posted on 2011-09-21
3
284 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

740 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