3

我正在尝试比较两个时间戳,如果它大于 x 秒的差异,则表示“离线”。这是我在小部件的 js 编辑器中的内容:

// Example: Convert temp from C to F and truncate to 2 decimal places.
// return (datasources["MyDatasource"].sensor.tempInF * 1.8 + 32).toFixed(2);
console.log("Checking Time Difference")
var timediff = (new Date) - datasources["ConsentDS"].Timestamp 
console.log(timediff)
if timediff > 1 * 60 * 1000 {
    return 1
} else {
    return 0
}

即使差异应该大于 30 秒,该指标也始终保持“在线”。它甚至没有像我期望的那样写入控制台。

我找不到任何文档,所以我什至不确定我是否应该返回 1 或 true 或大象 :(

4

1 回答 1

2

所以我的大部分问题是 javascript 语法,正如@Donut 和其他人可能立即注意到的那样。

这是工作版本:

var ts = new Date(datasources["ConsentDS"].Timestamp).getTime();
var ms = new Date().getTime();
var d = ms - ts;

if (d > 5 * 60 * 1000) {
    return 0;
} else {
    return 1;
}

如果当前时间减去数据上的时间戳超过 30 秒(30000 毫秒),则返回 0,即指标小部件上的“关闭”状态。

于 2015-07-15T21:53:26.370 回答