我有一个 jquery 函数,当用户每次滚动到下面时,它会加载更多帖子
这是 jquery 代码
$(function() {
var $timeline = $('#tupdate'),
$spinner = $('#loading').hide();
function loadMore() {
$(window).unbind('scroll.posts');
$spinner.show();
$.ajax({
url: "/ajax/loadmore?lastPost=" + $(".pointer:last").attr('id'),
success: function(html) {
if (html) {
$timeline.append(html);
$spinner.hide();
} else {
$spinner.hide();
bootbox.alert('No more post available');
}
$(window).bind('scroll.posts', scrollEvent);
}
});
}
//lastAddedLiveFunc();
$(window).bind('scroll.posts', scrollEvent);
function scrollEvent() {
var wintop = $(window).scrollTop(),
docheight = $(document).height(),
winheight = $(window).height();
var scrolltrigger = 0.95;
if ((wintop / (docheight - winheight)) > scrolltrigger) loadMore();
}
});
现在的问题是,每当用户向下滚动弹出窗口时,每次都会显示这会激怒用户,我如何才能只提醒一次?
这是我尝试过的
if (!g) {
var g = 1;
bootbox.alert('No more Post');
}
但每次我向下滚动时它仍然继续显示警报
最佳答案
您正在范围内声明g
。为了便于阅读,我已将其重命名为 showAlert
。
// Declare outside of `loadMore()`.
var showAlert = true; // True by default.
function loadMore(){
$(window).unbind('scroll.posts');
$spinner.show();
$.ajax({
url: "/ajax/loadmore?lastPost="+ $(".pointer:last").attr('id'),
success: function(html){
if(html){
$timeline.append(html);
$spinner.hide();
}else{
$spinner.hide();
if (showAlert)
{
bootbox.alert('No more post available');
showAlert = false; // don't show no more.
}
}
$(window).bind('scroll.posts',scrollEvent);
}
});
}
关于javascript - 如何在 jQuery 中仅显示一次警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37804812/