Javascript 禁用Jquery/JS函数
我有一个功能,每当用户滚动并点击页面底部时(如Facebook),就会从数据库加载新数据。现在的问题是,当用户滚动到底部时,向上滚动一点,然后再向下滚动到底部:这会调用函数两次,对吗?因此,将两次加载新帖子 我喜欢做的是在激活时临时禁用该功能,加载新数据,然后再次启用该功能 这是我的密码:Javascript 禁用Jquery/JS函数,javascript,jquery,jquery-events,Javascript,Jquery,Jquery Events,我有一个功能,每当用户滚动并点击页面底部时(如Facebook),就会从数据库加载新数据。现在的问题是,当用户滚动到底部时,向上滚动一点,然后再向下滚动到底部:这会调用函数两次,对吗?因此,将两次加载新帖子 我喜欢做的是在激活时临时禁用该功能,加载新数据,然后再次启用该功能 这是我的密码: function scroll_load() { $('#maincontainer').bind('scroll', function(){ If($(this).scrollTop()+$
function scroll_load() {
$('#maincontainer').bind('scroll', function(){
If($(this).scrollTop()+$(this).innerHeight()>=$(this)[0].scrollHeight){
//CODE THAT TEMPORARILY DISABLES THE SCROLLING FUNCTION UNTIL AJAX LOADS SUCCESSFULLY here--
//loads the new posts through ajax
$.ajax({
type: 'GET',
url: "getpost.php",
data: {'id':my_id},
success: function(data){
$('#dataview').append(data);
}
});
}
});
}
我希望这有帮助
var isScrolled = false;
function scroll_load() {
if(isScrolled == false){
$('#maincontainer').bind('scroll', function(){
If($(this).scrollTop()+$(this).innerHeight()>=$(this)[0].scrollHeight){
//CODE THAT TEMPORARILY DISABLES THE SCROLLING FUNCTION UNTIL AJAX LOADS SUCCESSFULLY here--
//loads the new posts through ajax
$.ajax({
type: 'GET',
url: "getpost.php",
data: {'id':my_id},
success: function(data){
$('#dataview').append(data);
}
});
}
});
IsScrolled = true;
}
}
您可以使用超时功能将IsScrolled值再次设置为False,我希望这会有所帮助
var isScrolled = false;
function scroll_load() {
if(isScrolled == false){
$('#maincontainer').bind('scroll', function(){
If($(this).scrollTop()+$(this).innerHeight()>=$(this)[0].scrollHeight){
//CODE THAT TEMPORARILY DISABLES THE SCROLLING FUNCTION UNTIL AJAX LOADS SUCCESSFULLY here--
//loads the new posts through ajax
$.ajax({
type: 'GET',
url: "getpost.php",
data: {'id':my_id},
success: function(data){
$('#dataview').append(data);
}
});
}
});
IsScrolled = true;
}
}
function scroll_load() {
var throttled = false;
var scrollDelay = 350;
$('#maincontainer').bind('scroll', function(){
If($(this).scrollTop()+$(this).innerHeight()>=$(this)[0].scrollHeight){
//CODE THAT TEMPORARILY DISABLES THE SCROLLING FUNCTION UNTIL AJAX LOADS SUCCESSFULLY here--
if (!throttled) {
throttled = true;
//loads the new posts through ajax
$.ajax({
type: 'GET',
url: "getpost.php",
data: {'id':my_id},
success: function(data){
$('#dataview').append(data);
}
});
// we don't allow to our function to execute more than once every X milliseconds
setTimeout(function (){
throttled = false;
}, scrollDelay);
}
}
});
}
您可以使用超时功能将IScrowled值再次设置为False,也可以使用标志。当您调用函数时,标志将为1,当您收到返回的数据时,标志将为0,如下所示:
function scroll_load() {
var throttled = false;
var scrollDelay = 350;
$('#maincontainer').bind('scroll', function(){
If($(this).scrollTop()+$(this).innerHeight()>=$(this)[0].scrollHeight){
//CODE THAT TEMPORARILY DISABLES THE SCROLLING FUNCTION UNTIL AJAX LOADS SUCCESSFULLY here--
if (!throttled) {
throttled = true;
//loads the new posts through ajax
$.ajax({
type: 'GET',
url: "getpost.php",
data: {'id':my_id},
success: function(data){
$('#dataview').append(data);
}
});
// we don't allow to our function to execute more than once every X milliseconds
setTimeout(function (){
throttled = false;
}, scrollDelay);
}
}
});
}
var flag = 0;
function scroll_load() {
$('#maincontainer').bind('scroll', function(){
if(flag) return;
else {
flag = 1;
If($(this).scrollTop()+$(this).innerHeight()>=$(this)[0].scrollHeight){
//loads the new posts through ajax
$.ajax({
type: 'GET',
url: "getpost.php",
data: {'id':my_id},
success: function(data){
flag = 0;
$('#dataview').append(data);
}
});
}
});
}
你可以用旗子。当您调用函数时,标志将为1,当您收到返回的数据时,标志将为0,如下所示:
var flag = 0;
function scroll_load() {
$('#maincontainer').bind('scroll', function(){
if(flag) return;
else {
flag = 1;
If($(this).scrollTop()+$(this).innerHeight()>=$(this)[0].scrollHeight){
//loads the new posts through ajax
$.ajax({
type: 'GET',
url: "getpost.php",
data: {'id':my_id},
success: function(data){
flag = 0;
$('#dataview').append(data);
}
});
}
});
}
搜索“去抖动”。搜索“去抖动”。