Javascript变量范围 - 未分配的值

时间:2015-06-02 19:10:49

标签: javascript arrays

我正在处理日历应用程序,但我无法将值设置为全局声明的变量,然后在另一个函数中访问这些值。

以下是一些代码。如果您需要更多请注释,我可以将其全部粘贴

var eventhold = [];
var eyears = [];
var emonth = [];
var eday = [];
var events;

var loadData = function() {
  $.ajax({
    type: "POST",
    url: "getevents.php"
  }).done(function(data) {

    events = JSON.parse(data); //returning json object

    for (i = 0; i < events.length; i++) {
      eventhold[i] = events[i].eDate; //returns 2015-06-15 for each object

      eyears[i] = eventhold[i].substring(0, 4); //trims to 2015
      emonth[i] = eventhold[i].substring(5, 7); //trims to 06
      emonth[i] = parseInt(emonth[i]); //change to int
      emonth[i] --; //decrement to 0 base
      eday[i] = eventhold[i].substring(8, 10); //trims to 08
      eday[i] = parseInt(eday[i]); //change to int
    }

  });
}

function test() {
  firstDay = new Date(oDate.cyear, oDate.cmonth, 1); //return date for the "current year/ current month"
  numDay = firstDay.getDay(); // find the starting day for calendar offset

  console.log(eyears.length);
  // var times = eyears.length;
  // console.log(times);

  for (i = 0; i < eyear.length; i++) {
    console.log("first for");
    if (oDate.cyear == eyears[i] && oDate.cmonth == emonth[i]) //none of these values are registering. Again this all works in the console.
    {
      console.log("inside if");
      $("#day" + eday[i + numDay]).append(events[i].eName + "<br>" + events[i].eTime); //adding numDay to match the calendar "offset"
    }
  }
}

当我在控制台中粘贴这个for循环时,它可以很好地工作,一切都输出正常。但我无法获得任何值到这个函数。任何帮助表示赞赏。让我知道如果有什么不清楚,我可以粘贴更多的代码,我只是希望不要压倒。

0 个答案:

没有答案