Hacked Files
Hacked Files

Reputation: 99

AJAX Refresh Div Not Functioning

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function() {
    setInterval(function() {
        $.get('points.txt', function(data){
            $('#show').html(data);
        },1000);
    });
});
</script>

<div id="show"></div>

I have the above script running, and in points.txt there's a number which keeps changing.

It's suppose to refresh the div every second.

Now, for some reason, the script ain't working. What am I doing wrong?

Upvotes: 2

Views: 44

Answers (2)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

Your second param of timing function is not given to the setInterval, and not the get. Also I guess your request is getting cached. Try this:

$(document).ready(function() {
  setInterval(function() {
    $.get('points.txt?' + (new Date).getTime(), function(data){
      $('#show').html(data);
    });
  }, 1000);
});

Or tell the AJAX, not to cache data:

$.ajaxSetup({
  cache: false
});

Upvotes: 3

dotnetom
dotnetom

Reputation: 24901

I believe there is incorrect formatting of your code. Parameter 1000 is added as third parameter to get function. Instead it should be like this:

$(document).ready(function() {
    setInterval(function() {
        $.get('points.txt', function(data){
            $('#show').html(data);
        });
    },1000);
});

Upvotes: 2

Related Questions