搜尋

首頁  >  問答  >  主體

javascript - 回呼函數哪寫錯了麼

function GetOrderList(tabIndex, dirc,callback) {
    var direction = 1, startNum = 0;
    if (tabIndex == 1) {
        if (handle1 == 1) {
            startNum = startMaxNum1;
            direction = 2;
        }
        else
            startNum = startMinNum1;
    }
    else if (tabIndex == 2) {
        if (handle2 == 1) {
            startNum = startMaxNum3;
            direction = 2;
        } else {
            startNum = startMinNum2;
        }
    } else if (tabIndex == 3) {
        if (handle3 == 1) {
            startNum = startMaxNum3;
            direction = 2;
        }
        else
            startNum = startMinNum3;
    } else {
        if (handle4 == 1) {
            startNum = startMaxNum4;
            direction = 2;
        }
        else
            startNum = startMinNum4;
    }
    if (dirc == 2) {
        direction = 2;
        startNum = 0;
    }
    $.post('/order/GetOrderListByCondition', {
        keyWord: $.trim($("#txtSearch").val()), tabIndex: tabIndex, direction: direction, startNum: startNum
    }, function (data) {
        if (data != null) {
            if (direction == 2) {
                $("#pOrder" + tabIndex).html("");
            }
            if (data.Orders.length >= 1) {
                if (direction == 1) {
                    if (tabIndex == 1) {
                        if (startMaxNum1 == 0) {
                            startMaxNum1 = data.Orders[0].Id;
                        }
                        startMinNum1 = data.Orders[data.Orders.length - 1].Id;
                    } else if (tabIndex == 2) {
                        if (startMaxNum2 == 0) {
                            startMaxNum2 = data.Orders[0].Id;
                        }
                        startMinNum2 = data.Orders[data.Orders.length - 1].Id;
                    } else if (tabIndex == 3) {
                        if (startMaxNum3 == 0) {
                            startMaxNum3 = data.Orders[0].Id;
                        }
                        startMinNum3 = data.Orders[data.Orders.length - 1].Id;
                    } else if (tabIndex == 4) {
                        if (startMaxNum4 == 0) {
                            startMaxNum4 = data.Orders[0].Id;
                        }
                        startMinNum4 = data.Orders[data.Orders.length - 1].Id;
                    }
                } else {
                    if (tabIndex == 1) {
                        if (startMinNum1 == 0) {
                            startMinNum1 = data.Orders[data.Orders.length - 1].Id;
                        }
                        startMaxNum1 = data.Orders[0].Id;
                    } else if (tabIndex == 2) {
                        if (startMinNum2 == 0) {
                            startMinNum2 = data.Orders[data.Orders.length - 1].Id;
                        }
                        startMaxNum2 = data.Orders[0].Id;
                    } else if (tabIndex == 3) {
                        if (startMinNum3 == 0) {
                            startMinNum3 = data.Orders[data.Orders.length - 1].Id;
                        }
                        startMaxNum3 = data.Orders[0].Id;
                    } else if (tabIndex == 4) {
                        if (startMinNum4 == 0) {
                            startMinNum4 = data.Orders[data.Orders.length - 1].Id;
                        }
                        startMaxNum4 = data.Orders[0].Id;
                    }
                }
                $(data.Orders).each(function (i) {
                    var orderstrs = "<a href='/order/ordersdetail?orderid=" + data.Orders[i].Id + "&PassKey=" + data.Orders[i].PassKey + "' class='linkDetail'><p class='listBox'> <p class='orderHeader'><ul><li><img src='/images/list.png'></li><li>订单号:<span data-name='orderCode' class='orderCode'>" + data.Orders[i].OrderCode + "</span></li></ul><span class='orderTime' data-name='orderTime'>" + ToJavaScriptDate(data.Orders[i].OrderTime) + "</span></p>";
                    if (data.OrdersDetail.length >= 1) {
                        var details = Enumerable.From(data.OrdersDetail).Where("x=>x.OrderId==" + data.Orders[i].Id).ToArray();
                        if (details != null && details.length >= 1) {
                            orderstrs += "<p class='orderContent'>";
                            $(details).each(function (j) {
                                var img = "";
                                if (details[j].Images != null && details[j].Images != "") {
                                    img = $.trim(details[j].Images).split(" ")[0];
                                }
                                else {
                                    img = "/Images/default_list_100_100.png";
                                }
                                orderstrs += "<p class='orderDetailBox'><img class='productImg' src='" + img + "'></span><span class='giftIcon'>" + (details[j].IsGift ? "<img src='/Images/zeng.png'/>" : "") + "</span><span class='productName' data-name='productName'>" + details[j].ProductName + "</span><ul><li>¥<span class='unitPrice' data-name='unitPrice'>" + details[j].UnitPrice + "</li><li>×<span class='num' data-name='num'>" + details[j].Num + "</span></li></ul></p>";
                            });
                        }
                        orderstrs += "</p>";
                    }
                    orderstrs += "<p class='statis'><ul><li>共<span class='productCount' data-name='productCount'>" + data.Orders[i].ProductCount + "</span>件商品</li><li>合计:¥<span class='payAmount' data-name='payAmount'>" + data.Orders[i].PayAmount + "</span></li></ul></p></p></a><p class='handle'>";
                    if (tabIndex == 2) {
                        orderstrs += "<button class='btn-primary' id='btnconfirm" + data.Orders[i].Id + "' onclick='confirmorder(" + data.Orders[i].Id + ",\"" + data.Orders[i].PassKey + "\")'>确认收货</button>";
                    }
                    if (tabIndex != 1) {
                        orderstrs += "<button class='btn-default'><a href='tel: " + data.Orders[i].PhoneNumber + "'>联系配送工</a></button>";
                    }
                    orderstrs += "<button class='btn-default'><a href='tel: " + data.Orders[i].MemberPhoneNumber + "'>联系业务员</a></button>";
                    $("#pOrder" + tabIndex).append(orderstrs + "</p>");
                });
            }
        }
        requestf5.style.opacity = 0;
        myScroll.refresh();
    });
    callback();
}

#
 GetOrderList(tabIndex,1,function () {
                   var activeHight = $(".swiper-slide").eq(swiper.activeIndex).height();
                  var containerHight = parseFloat($("#wrapper").height()) ;
                 if (activeHight <= containerHight)
                     activeHight = containerHight;
                $(".swiper-container").height(activeHight);
            });

哪裡錯了什麼 報錯 callback is not a function

我想大声告诉你我想大声告诉你2768 天前936

全部回覆(4)我來回復

  • 淡淡烟草味

    淡淡烟草味2017-05-19 10:46:22

    函數 () {

    雷雷

    回覆
    0
  • PHP中文网

    PHP中文网2017-05-19 10:46:22

    把錯誤訊息發出來看看.或是在函數頭部把callback log出來看看.

    回覆
    0
  • PHP中文网

    PHP中文网2017-05-19 10:46:22

    理解與使用Javascript中的回呼函數

    如 @sunshine999 所說,把callback()在外面定义,再将其作为参数传递给GetOrderList

    function callback() {
       var activeHight = $(".swiper-slide").eq(swiper.activeIndex).height();
       var containerHight = parseFloat($("#wrapper").height()) ;
       if (activeHight <= containerHight)
           activeHight = containerHight;
           $(".swiper-container").height(activeHight);
    }
                
    GetOrderList(tabIndex,1,callback); 

    在執行之前確保回調函數是一個函數
    在呼叫之前檢查作為參數被傳遞的回調函數確實是一個函數,這樣的做法是明智的。同時,這也是實現條件回呼函數的最佳時間。

    function GetOrderList(tabIndex,dirc,callback){
    ...
         //确保callback是一个函数    
        if(typeof callback === "function"){
            //调用它,既然我们已经确定了它是可调用的
              callback(options);
        }
    }

    我這回答最上面貼的那篇文章講的很詳細了。

    回覆
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-19 10:46:22

    試下`
    callback = function(){
    var activeHight = $(".swiper-slide").eq(swiper.activeIndex).height();
    var containerHight = parseFloat($("#wrapper")
    var containerHight = parseFloat($("#wrapper"). height()) ;

    if (activeHight <= containerHight)

    雷雷
    }

    `🎜

    回覆
    0
  • 取消回覆