编辑:使用新代码更新,无需轮询,只需在滚动时设置/重置标志。
演示
var isScrolling = false;
$(function() {
$('#scrollingDiv').on('scroll', function() {
isScrolling = true;
});
refreshTimer = setInterval(refreshContent, 5000);
function refreshContent() {
if (!isScrolling) {
$('#scrollingDiv').prepend('Latest Content <br />');//test code
//$('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container');
}
isScrolling = false;
}
});
---------- 旧帖 ----------
对 div 滚动事件进行简单的轮询就可以解决问题。见演示
var isScrolling = false;
var refreshTimer = null;
$(function() {
$('#scrollingDiv').on('scroll', function() {
isScrolling = true;
if (refreshTimer != null) {
clearInterval(refreshTimer);
refreshTimer = null;
}
});
//polling to see if still scrolling
var pollScrolling = setInterval(function() {
isScrolling = false;
if (refreshTimer == null) {
refreshTimer = setInterval(refreshContent, 5000);
}
}, 500);
//initialize timer
refreshTimer = setInterval(refreshContent, 5000);
function refreshContent() {
if (!isScrolling) {
$('#scrollingDiv').prepend('Latest Content <br />');
//$('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container');
}
}
});