DobotJr
DobotJr

Reputation: 4049

Disable auto refresh on div if user is scrolling (JQuery)

I've got a DIV #alerts_wrapper on my page that is being refreshed every 5 seconds like this:

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

I've got a max-height on the div set to 200px, and scrolling to auto. How can I stop the div from refreshing if the user if scrolling on this div? And then if the user stops scrolling, start refreshing again??

Thanks!

Upvotes: 1

Views: 2341

Answers (3)

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

Edit: Updated with new code, no need for polling just set/reset the flag when u scroll.

DEMO

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;
    }

});

---------- Old post ----------

A simple polling on div scroll event will do the trick. See DEMO

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');            
        }
    }

});

Upvotes: 2

kitti
kitti

Reputation: 14814

To do this, I think you'll need to implement custom scroll events as outlined here: http://james.padolsey.com/javascript/special-scroll-events-for-jquery/

Then you can create a global variable (or even better, in a closure with the interval code), let's call it var isScrolling = false. Create handlers for scrollstart and scrollstop:

jQuery(div).on( 'scrollstart', function( ) {
    isScrolling = true;
} );
jQuery(div).on( 'scrollstop', function( ) {
    isScrolling = false;
} );

Finally, check in your interval for the scrolling flag:

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

Upvotes: 0

Ayush
Ayush

Reputation: 42450

Use this Jquery plugin: scroll-startstop.events.jquery

Using the above mentioned plugin, you now have access to scrolling events, like this:

$('#yourdiv').bind('scrollstart', function(){
    //user is scrolling
});
$('#yourdiv').bind('scrollstop', function(){
    //user has finished scrolling
});

Use this in conjunction with a bool flag to know when to refresh the div.

Your final code should look something like this:

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); 

Upvotes: 3

Related Questions