0

I am having a strange problem, a part of jquery code is not working properly until I add breakpoint in firebug, this is the code

    function loadPreviousGameCount() {

        var $hdnTotalPreviousGamesCount = $('#<%=hdnTotalPreviousGamesCount.ClientID %>');

        // load page count only if it is not already loaded
        if ($hdnTotalPreviousGamesCount.val() == "-1") {
            $.post('MypageUrl&MemberId=<%=MemberId%>',
            {},
            function(response) {
                if (response.IsDone) {
                    $hdnTotalPreviousGamesCount.val(response.Data);
                } else {
                    $hdnTotalPreviousGamesCount.val(response.Message);
                }
            }, 'json');
        }
    }

and I am using this function to intiate paging

function initPagination() {

   loadPreviousGameCount();

   // I put breakpoint on the below line("var pageCount...")
   var pageCount = parseInt($('#<%=hdnTotalPreviousGamesCount.ClientID %>').val());

   // Create pagination element              
   $("#divPager").show().pagination(pageCount,
                   {
                      callback: loadPreviousGames,
                      num_edge_entries: 1,
                      items_per_page: 5
                   });
}

So what I am trying to do is, I am loading games count from server and saving it to a hidden field if it is not already been loaded, and that games count I am using for pagination. The code is working perfectly but only either from the second time or if I put a debug point in the very first time.

So I find the value written in hidden field inside initPagination function in two cases

  1. In the second call of initPagination function.

  2. If I put a debug point on the line I mentioned above in the first call.

Any clues?

3 Answers 3

2

you need to call initPagination() function after the responce from server.

$.post('MypageUrl&MemberId=<%=MemberId%>',
            {},
            function(response) {
                if (response.IsDone) {
                    $hdnTotalPreviousGamesCount.val(response.Data);
                } else {
                    $hdnTotalPreviousGamesCount.val(response.Message);
                }
            initPagination();

            }, 'json');

what is happening here is may be you are calling the function before loading the data. When you have a debug point it will delay sometime which enables the response to come before your function call.

Sign up to request clarification or add additional context in comments.

Comments

0

It seems that you've introduced some sort of race condition. I would start by analyzing your server requests and responses. You might want to add the initialize call to the callback function.

Comments

0

Finally Solved it like this, I know this is not the correct way but have started working I have removed loadPreviousGamesCount() function completely

 function initPagination() {

            var $hdnTotalPreviousGamesCount = $('#<%=hdnTotalPreviousGamesCount.ClientID %>');
            if ($hdnTotalPreviousGamesCount.val() == "-1") {
                $.post('<%= Page.ResolveUrl("~/AjaxPageRequestHandler.aspx") %>?class=imLeagues.Web.Members.Pages.BO.Member.MemberGamesBO&method=GetMemberPreviousGamesCount&MemberId=<%=MemberId%>',
                {},
                function(response) {
                    if (response.IsDone) {

                        $hdnTotalPreviousGamesCount.val(response.Data);

                        // Create pagination element              
                        $("#divPager").show().pagination(response.Data,
                                            {
                                                callback: loadPreviousGames,
                                                num_edge_entries: 1,
                                                items_per_page: 5
                                            });

                    } else {
                        $hdnTotalPreviousGamesCount.val(response.Message);
                    }
                }, 'json');
            }
            else {
                // Create pagination element
                $("#divPager").show().pagination($hdnTotalPreviousGamesCount.val(),
                                            {
                                                callback: loadPreviousGames,
                                                num_edge_entries: 1,
                                                items_per_page: 5
                                            });
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.