Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 意外循环中的jQuery Ajax调用_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 意外循环中的jQuery Ajax调用

Javascript 意外循环中的jQuery Ajax调用,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在开发一个应用程序,使用ajax调用加载内容。但在特定情况下,我在循环中得到一个ajax调用,但它以随机迭代结束。下面是从双击事件到最终ajax调用的顺序代码 //I use the double click event to fire the function $('.item-container').on('click', '.item', function(e) { var posX = e.pageX; var posY = e.pageY; var elem

我正在开发一个应用程序,使用ajax调用加载内容。但在特定情况下,我在循环中得到一个ajax调用,但它以随机迭代结束。下面是从双击事件到最终ajax调用的顺序代码

//I use the double click event to fire the function
$('.item-container').on('click', '.item', function(e) {
    var posX = e.pageX;
    var posY = e.pageY;
    var element = $(this);
    var that = this;
    setTimeout(function() {
        var dblclick = parseInt($(that).data('double'), 10);
        if (dblclick > 0) {
            $(that).data('double', dblclick-1);
        } else {
            showToolTip(posX,posY,element);
        }
    }, 300);
}).on('dblclick', '.item', function(e) {
    $(this).data('double', 2);
    addItemsToBill(this);
});


function addItemsToBill(selectedItem) {


var element = null;

$(".bill-item-list").each(function() {
    if ($(this).css("display") == "block") {
        element = $(this);
        return;
    }
});

if (element == null) {
    getSystemMessage(BILL_SELECT_VALIDATION_MSG);
    return;
}

if ($('#open-tab .bill-list').find(".bill-item-list").length == 0
        && $(element).find('.bill-item').length == 0) {
    getSystemMessage(BILL_SELECT_VALIDATION_MSG);
    return;
}

var bill = $('.sample-elements').find('.bill-item');
var clone = bill.clone();
var itemId = $(selectedItem).attr('id');
var billId = $(element).parent().attr('id');

if ($(selectedItem).attr('isopen') == '1') {

    $('.open-item-popup-overlay').lightbox_me( {
        centered : true,
        destroyOnClose : false,
    });

    $('.itmRate').val('');
    $('#itmAmt').html('0.00');
    $('.itemQty').text('1');

    $('.itm-cancel').click(function() {
        $('.open-item-popup-overlay').trigger('close');
    });

            //when the first run goes it's fine, bt after that, the looping starts from here. I double checked whether there is another code segment which fires this but there are none.
    $('.itm-ok').click(function(){
        if ($('.itmRate').val() == "") {
            getSystemMessage(AMOUNT_INPUT);
            return;
        }
                    //This function gets iterated multiple times if I run the same event for multiple times.
                    //The function is only used in this two locations and no any other places.
        billItemDrop(itemId, billId, clone, selectedItem, null,element, 1, true);
        return false;
    })

} else {
    billItemDrop(itemId, billId, clone, selectedItem, null, element, 0,true);
}
}

function billItemDrop(itemId, billId, billItemClone, doubleClickedItem,draggableHelper, catchElement, isOpen, isDoubleClick) {

var openItemQuantity =  stringToDouble($('.itemQty').val());
var openItemRate = stringToDouble($('.itmRate').val());
var openItemAmount = openItemQuantity * openItemRate;

var ajaxObject = 'itemId=' + itemId + '&billId=' + billId + '&qty='
        + openItemQuantity + '&rate=' + openItemRate + '&openItem='
        + isOpen;

ajaxCall(ADD_ITEM_TO_BILL,AJAX_POST,ajaxObject,function(response) {

            var responseObject = response.billDetail;
            var billTotal = responseObject.billTotal;

            var total = response.billDetail.rate * response.billDetail.qty;

            // Add Items to the bill failed
            if (response.status != "success") {
                getSystemMessage(ADD_ITEM_TO_PRINTED_BILL);
                return;
            }

            billItemClone.attr('itemId', responseObject.itemId);
            billItemClone.attr('billDetailId', responseObject.billDetailId);

            billItemClone.find('.bill-item-name').text(
                    responseObject.itemName);
            billItemClone.find('.bill-item-rate span').text(
                    responseObject.rate.toFixed(2));
            billItemClone.find('.bill-item-amount').text(
                    total.toFixed(2));
            billItemClone.find('.bill-item-qty span').text(responseObject.qty);

            if (responseObject.kotBotNo != null) {
                billItemClone.find('.kot-bot-num').text(
                        responseObject.kotBotNo);
            }

            billItemClone.draggable( {
                revert : false,
                addClasses : false,
                zIndex : 1,
                containment : "window",
                opacity : 0.5,
                cursor : "move",
                scroll : false,
                helper : function() {
                    return $(this).clone().appendTo('body').show();
                }
            });

            if (isDoubleClick == true
                    && (doubleClickedItem != undefined
                            || doubleClickedItem != null || doubelClickedItem != "")) {
                billItemClone.find('.bill-item-img img').attr('src',
                        $(doubleClickedItem).css('background-image'));
            } else if (isDoubleClick == false
                    && (draggableHelper != undefined
                            || drdraggableHelper != null || draggableHelper != "")) {
                billItemClone.find('.bill-item-img img').attr('src',
                        draggableHelper.css('background-image'));
            }

            if (catchElement.height() > 300) {
                catchElement.css('overflow-y', 'scroll');
                catchElement.css('height', '320px');
            }

            if (catchElement.find('.bill-item').length == 0) {
                billItemClone.insertBefore(
                        catchElement.find('.item-drop-point')).hide()
                        .fadeIn("slow");
            } else {
                billItemClone.insertBefore(
                        catchElement.find('.bill-item').first()).hide()
                        .fadeIn("slow");
            }

            catchElement.parent().find('.amount')
                    .html(billTotal.toFixed(2));

            if (draggableHelper != undefined || draggableHelper != null) {
                draggableHelper.hide('fade', function() {
                    draggableHelper.remove()
                });
            }

            $('.item-drop-point').removeClass('item-drop-hover');

        },false);
}

/**
 * 
 * Function for ajax calls - url : webservice url - type : ajax call type - data :
 * data that needs to be passed - callback : function that needs to be executed
 * 
 * when ajax call succeed
 * 
 * @param url
 * @param type
 * @param data
 * @param callback
 */
function ajaxCall(url, type, data, callback,async){

// Assign default value to async if its not specified
if (async===undefined) async = true;

$.ajax({
    url: url,
    type: type,
    data: data,
    dataType : "json",
    cache: false,
    beforeSend: function(x){
    if(x && x.overrideMimeType){
        x.overrideMimeType("application/json;charset=UTF-8");
        }
    },
    async : async,
    success:callback
});
}
函数被迭代,因此ajax调用也被迭代。我使用断点来跟踪迭代,但从点击事件的角度看,我无法定位在第一次点击事件后触发的位置。
代码中是否存在使函数迭代的错误。请帮助。

每次你打电话给addItemsToBill(这个)您正在将新事件处理程序添加到
$('.itm ok')

尝试删除对billItemDrop的调用(itemId、billId、clone、selectedItem、null、element、1、true)并将其替换为
console.log('Test'
),如果您发现每次单击都会生成多个控制台日志,则问题在于每次单击都由多个处理程序处理。如果是这种情况,并且您确实希望使用此结构,则必须在重新绑定它之前解除对单击处理程序的绑定

$('.itm-ok').off('click')
$('.itm-ok').on('click',function(){
        if ($('.itmRate').val() == "") {
            getSystemMessage(AMOUNT_INPUT);
            return;
        }
                    //This function gets iterated multiple times if I run the same event for multiple times.
                    //The function is only used in this two locations and no any other places.
        billItemDrop(itemId, billId, clone, selectedItem, null,element, 1, true);
        return false;
    })
你是个天才!:)谢谢你,伙计。。。它起作用了!我甚至多次绑定点击!