Detect if data is changed to jQuery.load ()

I am sure this exists, but I do not know what to look for:

I am updating the content through jQuery $('#somediv').load()and the other through $.ajax(). I would like to catch situations where new content is identical to old content and slows down the refresh rate.

I can imagine how to do this for the case ajax, but it looks like I would have to store a lot of data in memory.

But what about load?

+3
source share
2 answers

JQuery loadand functions $.ajax()are executed only once. If you are constantly updating, you should use a timer or a loop. Just clear the timer and set a new interval.

var refreshRateMs = 5000;
timerId = setInterval("refreshPage()", refreshRateMs);

function refreshPage() {
  $("#somediv").load(); // Or other refresh code
}

// Later, you need to reduce the refresh rate (increase the interval):
clearInterval(timerId)
refreshRateMs += 1000; // Make delay 1s longer
timerId = setInterval("refreshPage()", refreshRateMs);

.load(), , .load(). .load() , dom. $.get:

$.get('page_to_load.html', function(data) {
   var newContent = $('.result').html(data);
   if newContent == $('.oldContent') { // Whatever check is necessary
       $("#somediv").append(newContent);
   }
});
+2

Source: https://habr.com/ru/post/1789830/


All Articles