[英]Increment a counter once until value is greater than if statement (Javascript)
我有以下代碼:
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(即已執行一個步驟)時,它將對計數器進行添加步驟,直到z坐標為1或更大。 如何在整個持續時間內僅增加1,直到z坐標再次為1或更大?
您需要為此設置一個標志。
// 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;
}
只需在for循環中添加該條件:
if (step < 1) {
accSteps.push(parseInt(step));
}
for (i = 0; i < accSteps.length && step < 1; i++) {
zOutput++;
document.getElementById("zVal").innerHTML = zOutput;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.