我有以下邏輯只能在一定程度上擴展,換句話說,當達到某個閾值時,我必須為每種情況手動撰寫邏輯,最大的問題是我只參考了一個專案(thisCase)每次渲染都會增加。
任何想法如何能夠在不必為每種情況手動撰寫的情況下實作某些動態增量,這個函式是一個實用函式順便說一句。
let thisCase = 1
// this case increase after render
const increment = () => {
const incementThisCase = () => {
console.log(thisCase 1)
thisCase = thisCase 1
}
incementThisCase()
if (thisCase > 8 && thisCase < 16) {
alert(8)
}
if (thisCase > 16 && thisCase < 24) {
alert(16)
}
if (thisCase > 24 && thisCase < 32) {
alert(32)
}
};
<div onclick={increment()}>incrment</div>
uj5u.com熱心網友回復:
在此,您只需將 thisCase 除以 8,如下所示
let increment = (thisCase) => {
let i=Math.floor(thisCase/8) 1;
return i*8;
};
uj5u.com熱心網友回復:
這是我的嘗試,如果我理解正確,我認為它可以很好地解決您的問題。
注意:我在您帖子的評論中看到您增加了 1,出于演示目的,我的腳本增加了 4,但您可以更改該值以及“步驟”值(在您的問題中為 8)
var thisCase = 0;
var step = 8;
function increment(){
var currentStep = step;
thisCase = thisCase 4;
console.log('ThisCase = ' thisCase);
// testing logic
while(thisCase > currentStep){
const max = parseInt(currentStep) parseInt(step);
if(thisCase < max){
alert(thisCase ' is between ' currentStep ' and ' max);
return;
}
else currentStep = step;
}
};
<button onclick="increment()">Increment</button>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/486466.html
標籤:javascript
