【问题标题】:javascript return array not workingjavascript返回数组不起作用
【发布时间】:2012-10-20 13:33:36
【问题描述】:

这是创建数组的函数:

//retrieve calendar id's
function getCalendarId(){
    gapi.client.load('calendar', 'v3', function() {
        var request = gapi.client.calendar.calendarList.list();
        request.execute(function(resp) {
            var calendarID = [];
            for (var i = 0; i < resp.items.length; i++) {
                var ID = resp.items[i].id;
                var calendarSummary = resp.items[i].summary;
                var calendarLocation = resp.items[i].location;
                var calendarDescription = resp.items[i].description;
                if (calendarDescription && calendarDescription.indexOf("Resource Tracker") != -1) {
                        var index = calendarID.length;
                        calendarID[index] = ID;
                }//endif
            }//end for
            console.log(typeof calendarID);
            return calendarID;
        });//end request.execute function(resp)
    });//end client.load function
}//end getCalendarId

这是分配它的函数:

function submitEvents(numHorses){
   var startdate = $("#from").val();
   var enddate = $("#to").val();
   var horsetypes = [];
   var calid = [];
   console.log(typeof(calid));
   calid = getCalendarId();
   console.log(typeof(calid));
   for (i=0; i<numHorses;i++){
      horsetypes[i] = $( "#type"+i).val();
      addEvent(calid[i%calid.length], startdate, enddate, horsetypes[i]);
   }//end for           
  };//end submitEvents

当我检查类型时, calendarID 是一个对象,就在返回之前,而 calid 在我声明它时是一个对象,但是当我进行赋值时 calid=getCalendarID(); calid 的类型变为未定义。

我的头撞墙已经有一段时间了;任何帮助表示赞赏!

【问题讨论】:

    标签: javascript jquery arrays return typeof


    【解决方案1】:

    没有从最外层的函数返回;它没有返回值,因此设置为函数返回值的变量设置为未定义。内部匿名函数包含return,它可能对 API 有意义,也可能没有意义(可能没有)。

    真正的问题是对日历 API 的调用可能需要任意时间(它是异步的),因此您不能在单个(同步)函数中设置值并期望它能够工作。当您的请求进出远程服务器时,Javascript 不会停止执行,而您真的不希望它这样做。

    类似问题/答案: javascript function return not working

    由于您可能在其他地方使用getCalendarId,因此您可以考虑让它拥有自己的函数,并在 Google 日历函数返回时调用它:

    function getCalendarId(callback){                            // !!!
      gapi.client.load('calendar', 'v3', function() {
        var request = gapi.client.calendar.calendarList.list();
        request.execute(function(resp) {
          var calendarID = [];
          for (var i = 0; i < resp.items.length; i++) {
            // omitted for brevity
          }//end for
          console.log(typeof calendarID);
          callback(calendarID);                                  // !!!
        });//end request.execute function(resp)
      });//end client.load function
    }//end getCalendarId
    
    function submitEvents(numHorses){
      // omitted for brevity
      getCalendarId(function(calid) {                            // !!!
        console.log(typeof(calid));
        for (i=0; i<numHorses;i++){
          // omitted for brevity
        }//end for
      });
    };//end submitEvents
    

    【讨论】:

    • 杰夫,就是这样!我没有足够的代表来投票,否则我会!
    • @user1786543 要将其(或将来最有帮助的答案)标记为“已接受”,请click the checkbox under the voting arrows。它甚至比赞成票更好。乐意效劳! :)
    【解决方案2】:
    //retrieve calendar id's
    function getCalendarId(){
    var calendarID = [];
        gapi.client.load('calendar', 'v3', function() {
            var request = gapi.client.calendar.calendarList.list();
            request.execute(function(resp) {
    
                for (var i = 0; i < resp.items.length; i++) {
                    var ID = resp.items[i].id;
                    var calendarSummary = resp.items[i].summary;
                    var calendarLocation = resp.items[i].location;
                    var calendarDescription = resp.items[i].description;
                    if (calendarDescription && calendarDescription.indexOf("Resource Tracker") != -1) {
                            var index = calendarID.length;
                            calendarID[index] = ID;
                    }//endif
                }//end for
                console.log(typeof calendarID);
    
            });//end request.execute function(resp)
        });//end client.load function
    return calendarID;
    }//end getCalendarId
    

    这应该可行。 CalenderID 应该为被调用的外部函数声明。在您的代码中,您将在另一个函数中返回 calenderID load

    【讨论】:

    • 当且仅当load 函数和execute 函数被立即调用时才会起作用。如果任一调用实际上启动了对 Google 日历服务器的请求,calendarID 将保持为空,直到最里面的函数执行为止。
    • 当我这样做时,类型是正确的,但数组是空的。如果我在 for 循环中检查索引 0 它有一个值,但在 for 循环之外该索引是未定义的。知道为什么会这样吗?编辑:我刚刚阅读了 Jeff 关于 google API 调用的异步性质的帖子。接下来我会尝试他的建议。
    猜你喜欢
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    相关资源
    最近更新 更多