Function not working properly.
Unanswered
! nolikas posted this in #help-forum
![Avatar](https://cdn.discordapp.com/avatars/969215020384792597/7fea2f1cc79e13ce2cf636b8efbd2d11.webp?size=256)
! nolikasOP
I'm trying to make a CS:GO game called "Crash". The program generates a random number and my function is supposed to stop when the multiplier reaches a generated value. Tho for some reason the multiplier is going above generated value and only ends a few seconds later. Does anyone know why this could be happening?
![Image](https://cdn.discordapp.com/attachments/1222197458797465750/1222197459334467594/image.png?ex=66155715&is=6602e215&hm=ab680fd57eff1b8e8e34eb53fd47b40bf46ce352f0acb37d4a82722d323ae2da&)
9 Replies
![Avatar](https://cdn.discordapp.com/avatars/969215020384792597/7fea2f1cc79e13ce2cf636b8efbd2d11.webp?size=256)
! nolikasOP
So in this ex, program generated a value of
1.04
but multiplier managed to reach 1.18
export default function Crash() {
const [multiplier, setMultiplier] = useState<number>(1); // MULTIPLIER
const [isGrowing, setIsGrowing] = useState<boolean>(false); // RUNNING / OFF
const [speedMultiplier, setSpeedMultiplier] = useState<number>(0.4); // MULTIPLIER SPEED
const [betAmount, setBetAmount] = useState<number>(0); // BET AMOUNT
const [crashValue, setCrashValue] = useState<number>(1); // CRASH VALUE
useEffect(() => {
let lastUpdateTime = Date.now();
let lastSpeedUpdateTime = Date.now();
let animationFrameId: number;
const updateMultiplier = () => {
const currentTime = Date.now();
const elapsedTime = currentTime - lastUpdateTime;
const increment = elapsedTime * 0.00015 * speedMultiplier;
setMultiplier((prevMultiplier) => prevMultiplier + increment);
lastUpdateTime = currentTime;
// INCREASE MULTIPLIER
if (currentTime - lastSpeedUpdateTime >= 3000) {
setSpeedMultiplier((prevSpeedMultiplier) => prevSpeedMultiplier * 1.15);
lastSpeedUpdateTime = currentTime;
}
// UPDATE CRASH LIMIT
if (multiplier >= crashValue) {
handleStop();
} else {
animationFrameId = requestAnimationFrame(updateMultiplier);
}
};
if (isGrowing) {
animationFrameId = requestAnimationFrame(updateMultiplier);
}
return () => cancelAnimationFrame(animationFrameId);
}, [isGrowing, speedMultiplier]);
const generateCrashValue = () => {
const crash = Math.floor(Math.random() * (1000 - 100) + 100) / 100;
setCrashValue(crash);
};
const handleStart = () => {
generateCrashValue();
setIsGrowing(true);
};
const handleStop = () => {
setIsGrowing(false);
setSpeedMultiplier(0.4);
// RESET MULTIPLIER
setTimeout(() => {
setSpeedMultiplier(0.4);
setMultiplier(1);
}, 3000);
};
![Avatar](https://cdn.discordapp.com/avatars/969215020384792597/7fea2f1cc79e13ce2cf636b8efbd2d11.webp?size=256)
! nolikasOP
bump?
![Avatar](https://cdn.discordapp.com/avatars/184479429404262410/a_35db4fb943e84d39bb02d6ec0f1d0b7e.gif?size=256)
DirtyCajunRice | AppDir
useEffect isnt gonna rerender unless the values bound change. instead set an interval and do your checks in there.
![Avatar](https://cdn.discordapp.com/avatars/969215020384792597/7fea2f1cc79e13ce2cf636b8efbd2d11.webp?size=256)
! nolikasOP
Could you tell me how I do that?
![Avatar](https://cdn.discordapp.com/avatars/969215020384792597/7fea2f1cc79e13ce2cf636b8efbd2d11.webp?size=256)
! nolikasOP
If I add
multiplier
to the useEffect
's dependency array, it stops at the right multiplier, but then it's always growing at the same speed while what I want is that multiplier speed constantly keeps growing.![Avatar](https://cdn.discordapp.com/avatars/969215020384792597/7fea2f1cc79e13ce2cf636b8efbd2d11.webp?size=256)
! nolikasOP
What I have found is that if I add
multiplier
to dependencies array, this part of code is skipped:if (currentTime - lastSpeedUpdateTime >= 3000) {
setSpeedMultiplier((prevSpeedMultiplier) => prevSpeedMultiplier * 1.15);
lastSpeedUpdateTime = currentTime;
}
...
[isGrowing, speedMultiplier, multiplier]);
... because useEffect is used more frequently and less than 3 seconds had passed.........
![Avatar](https://cdn.discordapp.com/avatars/969215020384792597/7fea2f1cc79e13ce2cf636b8efbd2d11.webp?size=256)
! nolikasOP
How do I still make it be called every 3 seconds?