How can i check if function is done in jquery

43 Views Asked by At

here is my code:

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>

Is there any option to check, if autoscroll (.animate) is done ?

I want a push notification with push.js. If auto scroll was done, i want to add push:

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

There are 1 best solutions below

0
On

You can add a complete callback to the .animate() function:

$("#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

documentation