Php 如果用户正在滚动,则禁用div上的自动刷新(JQuery)

Php 如果用户正在滚动,则禁用div上的自动刷新(JQuery),php,jquery,css,scroll,refresh,Php,Jquery,Css,Scroll,Refresh,我的页面上有一个DIV#alerts(警报)包装器,每5秒刷新一次,如下所示: refresh_alerts = setInterval(function () { $('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container' ); }, 5000); $('#yourdiv').bind('scrollstart', function(){ //user is scrolling }); $

我的页面上有一个DIV#alerts(警报)包装器,每5秒刷新一次,如下所示:

refresh_alerts = setInterval(function () {
    $('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container' );
}, 5000); 
$('#yourdiv').bind('scrollstart', function(){
    //user is scrolling
});
$('#yourdiv').bind('scrollstop', function(){
    //user has finished scrolling
});
var isScrolling = false;

$('#yourdiv').bind('scrollstart', function(){
    isScrolling = true;
});
$('#yourdiv').bind('scrollstop', function(){
    isScrolling = false;
});

refresh_alerts = setInterval(function () {
    if (!isScrolling){
        $('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container' );
    }
}, 5000); 
我已经将div的最大高度设置为200px,并滚动到auto。如果用户在该div上滚动,如何阻止该div刷新?然后如果用户停止滚动,再次开始刷新

谢谢

使用这个Jquery插件:

使用上述插件,您现在可以访问滚动事件,如下所示:

refresh_alerts = setInterval(function () {
    $('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container' );
}, 5000); 
$('#yourdiv').bind('scrollstart', function(){
    //user is scrolling
});
$('#yourdiv').bind('scrollstop', function(){
    //user has finished scrolling
});
var isScrolling = false;

$('#yourdiv').bind('scrollstart', function(){
    isScrolling = true;
});
$('#yourdiv').bind('scrollstop', function(){
    isScrolling = false;
});

refresh_alerts = setInterval(function () {
    if (!isScrolling){
        $('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container' );
    }
}, 5000); 
与bool标志一起使用,可以知道何时刷新div

您的最终代码应该如下所示:

refresh_alerts = setInterval(function () {
    $('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container' );
}, 5000); 
$('#yourdiv').bind('scrollstart', function(){
    //user is scrolling
});
$('#yourdiv').bind('scrollstop', function(){
    //user has finished scrolling
});
var isScrolling = false;

$('#yourdiv').bind('scrollstart', function(){
    isScrolling = true;
});
$('#yourdiv').bind('scrollstop', function(){
    isScrolling = false;
});

refresh_alerts = setInterval(function () {
    if (!isScrolling){
        $('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container' );
    }
}, 5000); 
使用此Jquery插件:

使用上述插件,您现在可以访问滚动事件,如下所示:

refresh_alerts = setInterval(function () {
    $('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container' );
}, 5000); 
$('#yourdiv').bind('scrollstart', function(){
    //user is scrolling
});
$('#yourdiv').bind('scrollstop', function(){
    //user has finished scrolling
});
var isScrolling = false;

$('#yourdiv').bind('scrollstart', function(){
    isScrolling = true;
});
$('#yourdiv').bind('scrollstop', function(){
    isScrolling = false;
});

refresh_alerts = setInterval(function () {
    if (!isScrolling){
        $('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container' );
    }
}, 5000); 
与bool标志一起使用,可以知道何时刷新div

您的最终代码应该如下所示:

refresh_alerts = setInterval(function () {
    $('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container' );
}, 5000); 
$('#yourdiv').bind('scrollstart', function(){
    //user is scrolling
});
$('#yourdiv').bind('scrollstop', function(){
    //user has finished scrolling
});
var isScrolling = false;

$('#yourdiv').bind('scrollstart', function(){
    isScrolling = true;
});
$('#yourdiv').bind('scrollstop', function(){
    isScrolling = false;
});

refresh_alerts = setInterval(function () {
    if (!isScrolling){
        $('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container' );
    }
}, 5000); 

为此,我认为您需要实现如下所述的自定义滚动事件:

然后您可以创建一个全局变量(或者更好,在一个包含区间代码的闭包中),让我们称它为
var-isScrolling=false
。为
滚动开始
滚动停止
创建处理程序:

jQuery(div).on( 'scrollstart', function( ) {
    isScrolling = true;
} );
jQuery(div).on( 'scrollstop', function( ) {
    isScrolling = false;
} );
最后,检查滚动标志的间隔:

refresh_alerts = setInterval(function () {
    if( !isScrolling ) {
        $('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container' );
    }
}, 5000); 

为此,我认为您需要实现如下所述的自定义滚动事件:

然后您可以创建一个全局变量(或者更好,在一个包含区间代码的闭包中),让我们称它为
var-isScrolling=false
。为
滚动开始
滚动停止
创建处理程序:

jQuery(div).on( 'scrollstart', function( ) {
    isScrolling = true;
} );
jQuery(div).on( 'scrollstop', function( ) {
    isScrolling = false;
} );
最后,检查滚动标志的间隔:

refresh_alerts = setInterval(function () {
    if( !isScrolling ) {
        $('#leftside div#alerts_wrapper').load('staffhome.php #alerts_container' );
    }
}, 5000); 

编辑:使用新代码更新,无需轮询,只需在u滚动时设置/重置标志即可

var isScrolling=false;
$(函数(){
$('#scrollingDiv')。on('scroll',function(){
isScrolling=true;
});
refreshTimer=setInterval(refreshContent,5000);
函数refreshContent(){
如果(!IsCrolling){
$('#scrollingDiv').prepend('Latest Content
');//测试代码 //$(“#leftside div#alerts_wrapper”).load('staffhome.php#alerts_container'); } IsCrolling=假; } });
----------旧职位----------

一个简单的div scroll轮询事件就可以做到这一点。看

var isScrolling=false;
var refreshttimer=null;
$(函数(){
$('#scrollingDiv')。on('scroll',function(){
isScrolling=true;
if(刷新计时器!=null){
清除间隔(刷新计时器);
刷新计时器=空;
}
});
//轮询以查看是否仍在滚动
var pollScrolling=setInterval(函数(){
IsCrolling=假;
如果(刷新计时器==null){
refreshTimer=setInterval(refreshContent,5000);
}    
}, 500);
//初始化定时器
refreshTimer=setInterval(refreshContent,5000);
函数refreshContent(){
如果(!IsCrolling){
$('#scrollingDiv').prepend('Latest Content
'); //$(“#leftside div#alerts_wrapper”).load('staffhome.php#alerts_container'); } } });
编辑:使用新代码更新,无需轮询,只需在滚动时设置/重置标志即可

var isScrolling=false;
$(函数(){
$('#scrollingDiv')。on('scroll',function(){
isScrolling=true;
});
refreshTimer=setInterval(refreshContent,5000);
函数refreshContent(){
如果(!IsCrolling){
$('#scrollingDiv').prepend('Latest Content
');//测试代码 //$(“#leftside div#alerts_wrapper”).load('staffhome.php#alerts_container'); } IsCrolling=假; } });
----------旧职位----------

一个简单的div scroll轮询事件就可以做到这一点。看

var isScrolling=false;
var refreshttimer=null;
$(函数(){
$('#scrollingDiv')。on('scroll',function(){
isScrolling=true;
if(刷新计时器!=null){
清除间隔(刷新计时器);
刷新计时器=空;
}
});
//轮询以查看是否仍在滚动
var pollScrolling=setInterval(函数(){
IsCrolling=假;
如果(刷新计时器==null){
refreshTimer=setInterval(refreshContent,5000);
}    
}, 500);
//初始化定时器
refreshTimer=setInterval(refreshContent,5000);
函数refreshContent(){
如果(!IsCrolling){
$('#scrollingDiv').prepend('Latest Content
'); //$(“#leftside div#alerts_wrapper”).load('staffhome.php#alerts_container'); } } });
谢谢+1用于演示,无额外插件要求。@DobotJr请查看不需要任何轮询的更新代码。谢谢+1用于演示,无额外插件要求。@DobotJr请查看不需要任何轮询的更新代码。@xbonez;我在你的答案发布之前就开始回答了。简单的问题,简单的模式。@xbonez巧合;我在你的答案发布之前就开始回答了。简单的问题,简单的模式。