0

I am stuck again with a problem, let me explain it to you.

Inside the div I have fetched data with HTML SIMPLE DOM from other site. Like

<div id="data">.....</div>

It will refresh each and every time user will refresh the page. But I want something extra. What I wanna do is, refresh the div (inside which external data is fetched and added) periodically after 5 seconds.

Both the PHP SIMPLE HTML DOM script and this div is on same page.

Now I only need, any jquery or javascript code to refresh the div with data id after each 5 seconds with new data fron other site and all this without refreshing the whole page.


UPDATE:

I have used this code

$(document).ready( function() {
function getTheTime(){
$.get('http://your-domain/file.php',function(data,status){
        $('#data').html(data);
});
}
var refresh = setInterval(
        "getTheTime()",
        5000
    );
});

But the problem is very very strange, why it is not refreshing the div? Infact I have set alert for the interval but it also didn't worked. What the real problem is? Why it is not getting data from file.php and why actually it is not refreshing the div??

I am using latest jquery CDN. http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js

Ahmed Habib
  • 189
  • 11
  • does this help: http://stackoverflow.com/questions/15998853/refresh-a-div-with-php-data-every-10-seconds-using-jquery ? – Maximus2012 Aug 06 '13 at 13:57
  • I have tried this but it didn't helped because the fetched data and script is on the same page. – Ahmed Habib Aug 06 '13 at 13:58
  • 1
    you need: `setInterval`, ajax, and `$.load`. the data you want to load will need to be in a separate file. – user428517 Aug 06 '13 at 13:58
  • 3
    Include attempted solutions, why they didn't work, and the expected results. See also: [**Stack Overflow question checklist**](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist). As sgroves stated, you might want to do something like this: `var timer = setInterval(function(){$('#data').load('myExternalURI')},5000);` if you use jQuery. – Jeff Noel Aug 06 '13 at 14:03

3 Answers3

0
$(function() {
    setInterval(function(){
        $('#data').load('site.php');
    }, 5000);
});
mavili
  • 3,385
  • 4
  • 30
  • 46
0

Definitely a job for AJAX...

Since you say you're already using jQuery, I'll walk you through the steps quickly to get an AJAX function set up and run it on an interval.

Step 1: Create a PHP file which gets the data you want to put in the DIV...

Just make a PHP file and put the code in to get the data:

<?php echo "The time is " . date('Y-m-d H:i:s');

Step 2: Set up an AJAX function to get the data from that file...

function getTheTime(){
    $.get('http://yourdomain.com/ajax/getthetime.php',function(data,status){
        $('#data').text(data);
    });
}

(It would be possible to use the .load function instead, but it's far less flexible if you want to do anything with the data before putting it in the DIV).

Step 3: Call that function on an interval...

Next, we need to set up an interval to call the new function every 5 seconds.

$(function(){
    var refresh = setInterval(
        getTheTime(),
        5000
    );
});
Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
0

Instead of using setInterval to call the function every 5 seconds, you can use simple long polling technique to refresh your div every 5 seconds. The problem with setInterval is that if the ajax request doesn't complete in specified time (5 secs here) there will be the chain of ajax requests.

   function getTheTime(){
     $.ajax({
        type: "POST",
        url: "http://your-domain/file.php",            
        success: function(response) {
                   $('#data').html(response); //update your div
                 },
        complete: function(){
                   setTimeout(
                        getTheTime, /* Refresh time */
                        5000 /* ..after 5 seconds */
                    );                
                },
        error: function(XMLHttpRequest, textStatus, errorThrown){
                    //display you error message               
                },
        timeout: 5000 //Timeout is necessary to prevent chaining of unsuccessful ajax request    
      });        
    }
Community
  • 1
  • 1
Konsole
  • 3,447
  • 3
  • 29
  • 39