How to use Timer for calculation in React JS

I was working on a React project that I need the system to
do a calculation e.g If the user supply a value of Bomb to
be 10 and Energy to be 5. it means the number of bomb the
system is going to generate is 10 at every one minute in 5
times(the number of energy) which is equal to 50… so, at
every one one minute I need the system to keep doing the
addition i.e The number of bomb generated at one minute is 10,
the number of bomb generated at two minute is 20, the number
of bomb generated at three minute is 30 e.t.c until five
minute(which is the value of energy supplied) and after that
five minute the timer will stop. Any help on this please guys!

Hello @Abolaji welcome to the forum.

Here is how you would do that:

  • define states for interval_id, bombCount, Energy,currentEnergy=0;

  • when bomb and energy values are provided run
    interval_id = setInterval(handleState, 60*1000 )

  • the handleState function will run every minute and will check
    if(currentEnergy < energy)

    • if so increase bombCount by 10 and increase currentEnergy by 1
    • if not clear the interval

I hope it helps

hello and welcome to fcc forum :slight_smile:

same way as you would in “vanilla js” with only difference is you would use “hooks” in react such as useState and useEffect

if you are saying it needs to be continuously do that “counting” and not just once then i would recommend using “setTimeInterval()” as it will continuously run until its been cleared out

happy learning :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.