javascript - 回调函数哪儿写错了么
我想大声告诉你
我想大声告诉你 2017-05-19 10:44:22
[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 = "

  • 订单号:" + data.Orders[i].OrderCode + "
" + ToJavaScriptDate(data.Orders[i].OrderTime) + "

"; 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 += "

"; $(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 += "

" + (details[j].IsGift ? "" : "") + "" + details[j].ProductName + "

  • " + details[j].UnitPrice + "
  • ×" + details[j].Num + "

"; }); } orderstrs += "

"; } orderstrs += "

  • " + data.Orders[i].ProductCount + "件商品
  • 合计:¥" + data.Orders[i].PayAmount + "

"; if (tabIndex == 2) { orderstrs += ""; } if (tabIndex != 1) { orderstrs += ""; } orderstrs += ""; $("#pOrder" + tabIndex).append(orderstrs + "

"); }); } } 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

我想大声告诉你
我想大声告诉你

全部回复(4)
淡淡烟草味

function () {

               var activeHight = $(".swiper-slide").eq(swiper.activeIndex).height();
              var containerHight = parseFloat($("#wrapper").height()) ;
             if (activeHight <= containerHight)
                 activeHight = containerHight;
            $(".swiper-container").height(activeHight);
        }拿出来写试试,函数传参传的是值
PHP中文网

把错误信息发出来看看.或者在函数头部把callback log出来看看.

PHP中文网

理解与使用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);
    }
}

我这回答最上面贴的那篇文章讲的很详细了。

伊谢尔伦

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

   activeHight = containerHight;
   $(".swiper-container").height(activeHight);

}
`

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号