无效的开始“ NaN”-时间轴vis.js

时间:2018-07-16 10:19:22

标签: javascript vue.js vuejs2 vis.js vis.js-timeline

我不知道为什么,但是当我尝试打电话时

this.$refs.timeline.on('rangechange', function (start, end, byUser, event) {
    console.log('timechanged...')
})

每次Error: Invalid start "NaN",我都会收到此错误。

我在google上搜索了解决方案,但一无所获。

以下是时间轴的选项:

timeline: {
    stack: true,
    start: new Date(),
    end: new Date(1000 * 60 * 60 * 24 + (new Date()).valueOf()),
    min: new Date(2018, 0, 1),
    max: new Date(2019, 0, 1),
    zoomMin: 1000 * 27 * 24 * 24, // if you want to zoom more in then lower the 27
    zoomMax: 1000 * 60 * 60 * 24 * 31 * 3,
    orientation: 'top'
}

我已经登录vis.js脚本,这是怎么回事。它开始记录开始和结束日期,然后仅引发error NaN

enter image description here

这是我遇到错误的vis.js脚本代码。

  console.log('START', start)
  console.log('END', end)
  var newStart = start != null ? util.convert(start, 'Date').valueOf() : this.start,
      newEnd = end != null ? util.convert(end, 'Date').valueOf() : this.end,
      max = this.options.max != null ? util.convert(this.options.max, 'Date').valueOf() : null,
      min = this.options.min != null ? util.convert(this.options.min, 'Date').valueOf() : null,
      diff;
  // check for valid number
  if (isNaN(newStart) || newStart === null) {
    throw new Error('Invalid start "' + start + '"');
  }
  if (isNaN(newEnd) || newEnd === null) {
    throw new Error('Invalid end "' + end + '"');
  }

有人知道如何解决这个问题吗?谢谢。

2 个答案:

答案 0 :(得分:1)

这是因为new Date()创建了一个对象,而不是一个数字,并且您的函数期望日期是一个数字。因此NaN =不是数字。

[编辑]将测试逻辑更改为:

    console.log('START');
    console.dir(start);
    console.log('END');
    console.dir(end);
    var newStart = !isNaN(start) ? util.convert(start, 'Date').valueOf() 
                                 : this.start
       ,newEnd = !isNaN(end) ? util.convert(end, 'Date').valueOf() 
                                 : this.end
       ,max = this.options.max != null ? util.convert(this.options.max, 'Date').valueOf() : null
       ,min = this.options.min != null ? util.convert(this.options.min, 'Date').valueOf() : null
       ,diff;
    //check for valid number
    if ( isNaN(newStart) ) {
        throw new Error('Invalid start "' + start + '"');
    }
    if ( isNaN(newEnd) ) {
        throw new Error('Invalid end "' + end + '"');
    }

答案 1 :(得分:0)

尝试使用Date.now()-返回自1970年1月1日以来的毫秒数。

类似的东西:

timeline: {
    stack: true,
    start: Date.now(),
    end: Date.now() + (1000 * 60 * 60 * 24),
    min: new Date(2018, 0, 1),
    max: new Date(2019, 0, 1),
    zoomMin: 1000 * 27 * 24 * 24, // if you want to zoom more in then lower the 27
    zoomMax: 1000 * 60 * 60 * 24 * 31 * 3,
    orientation: 'top'
}

如果start | end应该是您可以执行的Date对象(而不是数字):

timeline: {
    stack: true,
    start: new Date(),
    end: new Date(Date.now() + (1000 * 60 * 60 * 24)),
    min: new Date(2018, 0, 1),
    max: new Date(2019, 0, 1),
    zoomMin: 1000 * 27 * 24 * 24, // if you want to zoom more in then lower the 27
    zoomMax: 1000 * 60 * 60 * 24 * 31 * 3,
    orientation: 'top'
}