我有以下代码:
if (step < 1) {
accSteps.push(parseInt(step));
}
for (i = 0; i < accSteps.length; i++) {
zOutput++;
document.getElementById("zVal").innerHTML = zOutput;
}
step
变量是从mbed加速度计z坐标计算步数。目前,当z坐标<1时如图1所示(即已经进行了一步),它将继续向计数器添加步骤,直到z坐标为1或更大。如何在整个持续时间内仅增加1,直到z坐标再次为1或更大?
答案 0 :(得分:0)
您需要为此设置一个标志。
// Put this flag somewhere only executed once
var hasRegisteredStep = false;
if (step < 1 && hasRegisteredStep === false) {
accSteps.push(parseInt(step));
hasRegisteredStep = true;
} else if(step > 0) {
hasRegisteredStep = false; // Reset the flag if greater than 0
}
for (i = 0; i < accSteps.length; i++) {
zOutput++;
document.getElementById("zVal").innerHTML = zOutput;
}
答案 1 :(得分:-1)
只需在for循环中添加该条件:
*