简体   繁体   中英

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);

   }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM