0

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

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

啊!!!

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(moment(data.events.event[x].start_time).format("YYYY"));

// 但是这里说 moment 返回 null

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) { } 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);
  });   }
4

2 回答 2

1

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

于 2013-10-04T06:38:58.357 回答
0

啊,循环中的闭包,你能发现下面两个代码片段的区别吗?

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]));
  }
}
于 2013-10-04T06:40:07.113 回答