【问题标题】:Invalid start "NaN" - timeline vis.js无效的开始“NaN” - 时间线vis.js
【发布时间】:2018-07-16 10:19:22
【问题描述】:

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

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

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

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

以下是时间线选项:

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

这是出现错误的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 + '"');
  }

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

【问题讨论】:

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


    【解决方案1】:

    这是因为 new Date() 创建了一个对象,而不是一个数字,并且您的函数期望 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 + '"');
        }
    

    【讨论】:

    • 尝试 (new Date()).getTime(),将返回一个 EPOCH 时间 w3schools.com/jsref/jsref_gettime.asp,自 1970 年以来的毫秒数。
    • 它不是那样工作的。我已经用一张图片和 vis.js 代码更新了我的问题,我得到了错误。可以看看吗?
    • @Reo93,不要与 null 进行比较,而是将您的测试替换为“isNaN( ref )”,其中 ref 是要测试的项目。如果 isNaN 返回 true,则参数不是包含“null”的数字。
    • 测试和参考是什么意思?
    • 我做到了。我正在制作一个 vue 应用程序,所以首先我使用了 vue2vis,我认为这个包不起作用。所以我需要用普通的 vis.js 来制作它。不过还是谢谢你!
    【解决方案2】:

    尝试使用 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 应该是您可以执行的日期对象(而不是数字):

    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'
    }
    

    【讨论】:

    • 它不是那样工作的。我已经用一张图片和 vis.js 代码更新了我的问题,我得到了错误。可以看看吗?
    猜你喜欢
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 2013-09-24
    • 2017-01-08
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多