Vis.js聚焦于“当前时间”(红线)

Vis.js focus on the Current Time (Red Line)

本文关键字:当前时间 红线 时间 聚焦 js Vis      更新时间:2023-09-26

如何更改Vis.js时间线,使其始终关注当前时间(红线),以便我知道时间线中现在发生了什么。根据我有多远或缩放级别,时间线应该向左或向右平移,并集中在当前的时间轴上。

我在这里考虑两个选项:要么运行一个setInterval(),每15分钟更新一次并关注当前时间,要么在页面上有一个手动按钮。

从我的研究来看,到目前为止,我发现这是一个与按钮有关的问题,显然还不起作用:

document.getElementById('focusNow').onclick = function() {
      timeline.focus(timeline.currentTime.bar)
          };

我知道这是一个老问题,但我能像这样解决它:

var refresh = 10000;
timeline = new vis.Timeline(container, data, options);
function updateTimeline(refresh) {
    setTimeout(function(){
        var current = new Date();
        timeline.moveTo(current);
        updateTimeline(refresh)
    }, refresh);
}
updateTimeline(refresh);

updateTimeline函数每10秒运行一次,并移动时间线,使当前时间居中(使用moveTo()函数),从而保持红线居中。一旦我的项目完成,我将使用github链接进行更新。