Page spending a lot of time before processing href of anchor tag after anchor is clicked

时间:2015-12-04 21:19:11

标签: php jquery ajax anchor href

my apologies if its an incomplete or irrelevant question but I am having a big problem in my code. By using ajax I call a list of hotel rooms using an API. Now I call separate asynchronous ajax requests using JQuery to get cancellation policy of these rooms. The ajax requests are going to single PHP script on the server. If cancellation policy is present then show the book button for the room and if cancellation policy is not present then hide the room.

Now the issue is that suppose there are 5 rooms and one room returns cancellation policy 1st I show its book button which is an anchor tag with href to checkout page. But if I click on this anchor tag the page takes 15-20 seconds more before it goes to the href location. Kindly let me know what can I do to reduce this time or what can I do to check where my page is spending these 15-20 seconds.

If needed I can provide links to test this module on my staging link. Thanks.

var xhrRequests = [];

function filterByCancellation(){
    $( ".hotel_package_row:visible" ).each(function( index ) {
        var cancellation_obj = $(this).find( ".cancellationText" );
        var pack_price = 0;
        var hotel_price = 0;
        if ($(cancellation_obj).text()=="") {
            var hotelid = $(cancellation_obj).prev("a").data( "hotelid"),
                packid  = $(cancellation_obj).prev("a").data( "packid"),
                cancel  = $(cancellation_obj);
            if(!$('#anc-'+packid).is(':visible') && $('#inp-'+packid).val()=="0"){
                $('#inp-'+packid).val("1");
                cancel.html('').slideToggle(function(){
                    var data = { hotelid: hotelid, packid: packid };
                    pack_price = parseInt($('#packprice_'+packid).val());
                    var xhr = $.ajax({
                      type: "POST",
                      url: "location_penny.php?section=cancellationData",
                      data: data,
                      success: function(result) {
                        //cancel.html(result);
                        if(result.indexOf('<div style="display:none;">') > -1){
                            $(cancellation_obj).parents('.hotel_package_row').html('');
                        }else{
                            hotel_price = parseInt($('#'+hotelid).find('.currency-sign-before').html());
                            if($("#price_update_"+hotelid).val()=='0'){
                                //alert("hotel price "+hotel_price+" updating for the first time with package "+pack_price);
                                $('#'+hotelid).find('.currency-sign-before').html(pack_price);
                                $("#price_update_"+hotelid).val("1");
                            }
                            if(pack_price<=hotel_price){
                                //alert("hotel price "+hotel_price+" is greater than current package price "+pack_price);
                                $('#'+hotelid).find('.currency-sign-before').html(pack_price);
                            }
                            $('#img-'+packid).hide();
                            $('#anc-'+packid).show();
                        }
                      },
                      async:true
                    });
                    xhrRequests.push(xhr);
                });
            }
        }
    });
}

function cancelXhrRequests(){
    for (var i = 0; i < xhrRequests.length; i++) {
        //xhrRequests[i].abort();
    }
}

0 个答案:

没有答案
相关问题