是否有一种方法可以限制我使用JavaScript从传感器读取数据的时间?



我正在尝试使用我的FitBit加速数据填充CSV文件。下面是我到目前为止的代码:

import { display } from "display";
import * as document from "document";
const accelLabel = document.getElementById("accel-label");
const accelData = document.getElementById("accel-data");
const header = ["X","Y","Z"]
const delimiter = ', '
let newheader = header.join(delimiter)+'n';
let csv = newheader;
const sensors = [];
if (Accelerometer) {
const accel = new Accelerometer({ frequency: 1 });
//const obj = {name: "John", age: 30, city: "New York"};
accel.addEventListener("reading", () => { //put your reading into here //toFixed is the same as round(x,1) //? is like an if else
let data = [accel.x, accel.y, accel.z];
csv += data.join(delimiter)+"n";

});
sensors.push(accel);
accel.start();
} else {
accelLabel.style.display = "none";
accelData.style.display = "none";
}

我只希望它填充文件约10秒。有没有办法限制这个程序的运行时间?

有可能是我可以使用的库,但我是在FitBit Studio中编写的,我不确定这会如何影响我可以/不可以使用的库。

setTimeout()工作正常。我稍微修改了一下,使它看起来更像这样:

...
function accelread(){
accel.addEventListener("reading", () => {
//put your reading into here //toFixed is the same as round(x,1) //? is like an if else
let data = [accel.x, accel.y, accel.z]; //setTimeout()
csv += data.join(delimiter)+"n";
});
}; 
if (Accelerometer) {
const accel = new Accelerometer({ frequency: 1 });
setTimeout(accelread(),3000);
//const obj = {name: "John", age: 30, city: "New York"};
});
...

这不是一个成品,我还没有办法测试它,因为我用来测试应用程序的FitBit模拟器实际上没有任何加速度数据,但这个想法是成立的。

相关内容

  • 没有找到相关文章

最新更新