Farer
Farer

Reputation: 25

Run function if data changed

I am new in jQuery and can't figure how to succesfully run the script I use in my pages. The problem is I need to fade in and out text only if there are changes in my load.php file but not constantly like it is now, please help anyone... Many thanks!

Here is my code:

    $(document).ready( function(){
$('#auto').load('load.php');

refresh();

});



function refresh()
{

    setTimeout(function() {


      $('#auto').fadeOut('fast').load('load.php').fadeIn('fast');

      refresh();
       }, 2000);

   }

Upvotes: 0

Views: 224

Answers (1)

puddi
puddi

Reputation: 811

Try this:

function refresh()
{
  setTimeout(function() {
    $.get('load.php', function(data) {
      if (data !== $('#auto').html()) {
         $('#auto').fadeOut('fast').html(data).fadeIn('fast');
      }
      refresh();
    });
  }, 2000);
}

This makes the request outside of load(), and only fades in/out + replaces the content if it's different.

Upvotes: 0

Related Questions