使用 fadeIn() 和 fadeOut() 对 AJAX 响应进行动画处理


Using fadeIn() and fadeOut() to animate AJAX response

在使用 ajax get 从 php 文件中获取结果(它只是一个小数)时,我想淡出旧结果并淡入新结果。

这是我目前尝试这样做的尝试,但问题是它会每 30 秒淡入和淡出一次,而不是在数据发生变化时。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
  function update() {
    $.ajax({
    type: "GET",
    url: "balancefetch.php",             
    dataType: "html",            
    success: function(response){                    
        $("#response1").html(response); 
    }
 });
}
    setInterval(update, 30000);
    update();
    setInterval(fadein, 30000);
 $("#response1").fadeIn();
    setInterval(fadeout, 30000);
 $("#response1").fadeOut();
});

您应该将成功回调更改为

success: function(response){    
  if ($("#response1").html() != response) {
    $("#response1").fadeOut(200, function() {
      $("#response1").html(response); 
      $("#response1").fadeIn();
    });
  }
}

并且只有

setInterval(update, 30000);
update();