javascript - 我如何检查函数是否在 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();
}
});
解决方案
您可以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
推荐阅读
- javascript - Wix 代码/为什么全选按钮不全选?
- azure-data-factory - Dynamic name of Spark notebook in Notebook activity
- java - What is the minimum number of test cases for edge and condition coverage in this example?
- python - 在 Python Selenium (Mac OS) 中使用向下箭头键
- macos - 如何更改在 pgrep 中显示的应用程序名称?
- python - 带有读取和随机模块的 Python 密码生成器
- javascript - Binance - 通过 WebSockets 获取所选硬币的当前价格
- javascript - 如何将承诺转换为异步等待
- javascript - 从 Promise 返回一个特定的字符串
- python - 在python中隐藏子进程搜索信息