首页 > 解决方案 > 我如何检查函数是否在 jquery 中完成

问题描述

这是我的代码:

function loadLog() {
  var oldscrollHeight = $("#chatbox")[0].scrollHeight - 20; //Scroll height before the request

  $.ajax({
    url: "log.html",
    cache: false,
    success: function(html)

    {
      $("#chatbox").html(html); //Insert chat log into the #chatbox div


      //Auto-scroll           
      var newscrollHeight = $("#chatbox")[0].scrollHeight - 20; //Scroll height after the request
      if (newscrollHeight > oldscrollHeight) {
        $("#chatbox").animate({
          scrollTop: newscrollHeight
        }, 'normal'); //Autoscroll to bottom of div

      }

    }
  });
}

setInterval(loadLog, 500);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

如果自动滚动(.animate)完成,是否有任何选项可以检查?

我想要一个带有 push.js 的推送通知。如果自动滚动完成,我想添加推送:

 Push.create("test",{
        body: "This is example of Push.js Tutorial",
        icon: '/Logo_small.png',
        timeout: 2000,
        onClick: function () {
            window.focus();
            this.close();
        }
    });

标签: javascriptjqueryajaxpush.js

解决方案


您可以complete向函数添加回调.animate()

$("#chatbox").animate({
  scrollTop: newscrollHeight
}, 'normal', function(){  // This function is a callback called on animation complete
  console.log("Animate is done");
}); //Autoscroll to bottom of div

文件


推荐阅读