Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only update div content if the content is new

I recently coded this code below that updates my div every 0.5 seconds (500ms) and I want it to only update if you have new content for the box

function refreshOnline() {
    $('#toupdate').load('http://frosthotel.co.uk/refreshstats', function() {
        $(this).unwrap();
    });
}

refreshOnline();
setInterval(function() {
    refreshOnline();
}, 500);
like image 715
Daniel Dean Avatar asked Dec 19 '25 06:12

Daniel Dean


1 Answers

You can use $.get, $.post or $.ajax instead of $.load and compare the return value on ajax callback function and update if necessary.

function refreshOnline() {
    $.get('http://frosthotel.co.uk/refreshstats', function(data) {
        if(data!=$('#toupdate').html()) $('#toupdate').html(data);
    });
}
like image 102
Ashkan Mobayen Khiabani Avatar answered Dec 20 '25 19:12

Ashkan Mobayen Khiabani