Deepti Thakur
Deepti Thakur

Reputation: 38

Give delay after each call for function called on scroll event

I am calling checkscroll() on scroll event, which is making Ajax requests continuously. I want to Provide some delay between calls to checkscroll(), so that my first Ajax request gets completed before second time checkscroll() gets called and make ajax request. Here is my code:

function scroll(){ $(window).bind('scroll',checkScroll);}
        // calling checkScroll()   on scroll
function refresh(){flag = 1;}

function checkScroll() {


if (flag==1){ $(window).unbind('scroll');}

if(nearBottomOfPage() == 0)
{

  currentPage ++;

  xhr = $.ajax(
    {
        url : '/ideabank?page=' + currentPage,
        beforeSend: function() {
                $('#loading').show()
            },
            complete: function(){
                $('#loading').hide()
            },


        success : function(){ "can I do something here"}

    } );
}

}

Upvotes: 0

Views: 472

Answers (2)

Deepti Thakur
Deepti Thakur

Reputation: 38

Thanks People, Its done I have given setTimeout to ajax.Posting code snippet for better understanding.

     setTimeout( function() {
        $.ajax({
            type: "GET",

            url: '/ideabank?page=' + currentPage,

            beforeSend: function() {
                $('#loading').show()
            },

            success: function(html) {
                $('#loading').hide()
            }
        });
    }, 500 ); 

Upvotes: 1

Needhi Agrawal
Needhi Agrawal

Reputation: 1326

Try this code:

function scroll(){ 
 $(window).bind('scroll',function(){
  setTimeOut(checkScroll,1000);
});
}

Upvotes: 1

Related Questions