for循环中的函数和数组创建错误

时间:2013-10-04 06:30:02

标签: javascript arrays for-loop momentjs

我正在使用一个名为moment.js的JS库来查找和消除start_time和stop_time之间存在较大差距的事件。我不断收到这个错误 -

TypeError:moment(...)为null var stopmoment1 = moment(data.events.event [x] .stop_time).format(“YYYY”);

AAA !!!

  

for(var x = 0; x< data.page_size; x ++){

var position = new google.maps.LatLng(
  data.events.event[x]['latitude'],
  data.events.event[x]['longitude']);
     

//在控制台中返回年份

     

的console.log(时刻(data.events.event [X] .start_time).format( “YYYY”));

     

//但是在这里它表示时刻返回null

     

var startmoment1 =   矩(data.events.event [X] .start_time).format( “YYYY”);   var startmoment2 = moment(data.events.event [x] .start_time).format(“MM”);   var startmoment3 = moment(data.events.event [x] .start_time).format(“DD”);

     

var startmoment = moment([startmoment1,startmoment2,startmoment3]);

     

var stopmoment1 = moment(data.events.event [x] .stop_time).format(“YYYY”);   var stopmoment2 = moment(data.events.event [x] .stop_time).format(“MM”);   var stopmoment3 = moment(data.events.event [x] .stop_time).format(“DD”);

     

var stopmoment = moment([stopmoment1,stopmoment2,stopmoment3]);

     

if(startmoment.diff(stopmoment,'days')> 7){} else {

if (moment().format("YYYYMMDD") == moment(data.events.event[x].start_time).format("YYYYMMDD")) {

  marker.push(new google.maps.Marker({
    position: position,
    map: map,
    icon: image
  }));

  marker[x].x = x;//make the marker aware of its own index

  google.maps.event.addListener(marker[x], 'click', function() {
    var div = document.createElement('div');
    div.setAttribute("id", "tabs-min");
    div.innerHTML = content[this.x];
    $(div).tabs();
    infowindow.setContent(div); 
    infowindow.open(map, this);
  });


} else {
  marker.push(new google.maps.Marker({
    position: position,
    map: map,
    icon: image1
  }));

  marker[x].x = x;//make the marker aware of its own index

  google.maps.event.addListener(marker[x], 'click', function() {
    var div = document.createElement('div');
    div.setAttribute("id", "tabs-min");
    div.innerHTML = content[this.x];
    $(div).tabs();
    infowindow.setContent(div); 
    infowindow.open(map, this);
  });   }

2 个答案:

答案 0 :(得分:1)

执行alert或console.writeline或调试它以找出data.events.event [x] .stop_time的值。
您可能正在将未定义或空字符串传递给导致异常的时刻函数。

答案 1 :(得分:0)

啊,closures在循环中,你能发现以下两段代码片段的差异吗?

for(i=0;i<10;i++){
  setTimeout(function(){
    console.log(i);//logs 10 every time
  },10);
}

正确的

for(i=0;i<10;i++){
  setTimeout((function(i){
    return function(){
      console.log(i);//logs 0,1,2,3,4...
    };
  }(i)),10);
}

<强> [更新]

我重新格式化了你的代码,删除了重复代码,添加了日志,甚至将监听器移动到一个单独的对象中,这样你就可以调用它并传递闭包所需的变量,而不会在那里捕获可能的大变量,并且具有更强大的事件处理程序附件在循环中:

var eventListeners={
  markerClick:function(myContent){
      return function(e) {
        var div = document.createElement('div');
        div.setAttribute("id", "tabs-min");
        div.innerHTML = myContent;
        $(div).tabs();
        infowindow.setContent(div);
        infowindow.open(map, this);
      };
    }
};

for (var x = 0; x < data.event.length; x++) {
  var position = new google.maps.LatLng(
          data.events.event[x]['latitude'],
          data.events.event[x]['longitude']);

  console.log("moment is:",moment);
  console.log("data.events is:", data.events);
  console.log("x is:",x);
  console.log("event at x is:",data.events.event[x]);
  console.log("start time is:",data.events.event[x].start_time);
  var startmoment1 = moment(data.events.event[x].start_time).format("YYYY");
  var startmoment2 = moment(data.events.event[x].start_time).format("MM");
  var startmoment3 = moment(data.events.event[x].start_time).format("DD");
  var startmoment = moment([startmoment1, startmoment2, startmoment3]);

  var stopmoment1 = moment(data.events.event[x].stop_time).format("YYYY");
  var stopmoment2 = moment(data.events.event[x].stop_time).format("MM");
  var stopmoment3 = moment(data.events.event[x].stop_time).format("DD");
  var stopmoment = moment([stopmoment1, stopmoment2, stopmoment3]);

  if (!(startmoment.diff(stopmoment, 'days') > 7)) {
    marker.push(new google.maps.Marker({
      position: position,
      map: map,
  //no need for the if statement, only thing different is the image
  //but you are repating a lot of other code like adding and defining the handler
      icon: (moment().format("YYYYMMDD") ===
              moment(data.events.event[x].start_time).format("YYYYMMDD")) ?
              image : image1
    }));
    google.maps.event.addListener(marker[x], 'click', 
      eventListeners.markerClick(content[x]));
  }
}