I cant to figure out how my timer works

Hi I am making a Game and I cant seem to figure out how to sort certain things… My problem is that I have a timer and buttons with functions… I made images swap with if and else if, but I want the timer to stop at 0… And inserting a Break to my loop doesn’t seem to work… or the counter vanishes from the html page… It made sense in my head but it cant seem to figure it out through googleing

I will insert images of my code, can someone help

Debugging from images is really difficult, you can fiddle with the code, you can’t run it, you can’t see where it goes wrong

Then how should I post it ? or Where so you can help me?

this i will put a codepen link now

Is there html code? surely there must be, the best way we can help is we can replicate the problem. You can also make a bin/fiddle on sites like jsbin, jsfiddle or codepen if you find that easier

This is the codepen link, the images wont load, i have no idea how to but the main problem works… The timer flows past the 0 to the negative numbers. Also I want the function to like add a random number, but I cant figure out a formula with ( math. Random ) and math. floor … I know what could have but I cant seem to execute them nicely at the moment.

i don’t you use clearInterval to stop setInterval anywhere, as such the interval continues endlessly.

You would need to upload the image, and then copy the image location. You can only uses image from the web in codepen

there are dozen things you can do, unless you can tell me what you really want, its going to be very difficult

Oh okay, i will take a look at clearinterval Thank you

I want my food/water/love button to add a random number for example

all 3 buttons add +5 because I said that but I want them to be a random number between with min2 and max 6 but something like etc etc = 5 + 5 + 5 / 4 = ( number with deci ) make that a whole number with floor.

Or I just want them not to be generic + 5 … hard to explain. I think i saw something on a tutorial once, I will go after that.
but the the important thing is that the timer needed to stop at 0 and then its Game over… so the clearinterval might work i guess… I will try thank you

but that is exactly what Math.floor() does?

i don’t have much time tomorrow, maybe the day after tomorrow that i can look into it

Thank you, i will just play with it for a little longer thank you SO much !

clearInterval is clearly what you need to stop the counter.

Did you figure it out?

Nope it does not seem to work… I cleared up a few pieces of code, like the loop and removed a function… it still works but adding the clearinterval does not work…

check documentation:


you need to pass intervalID to clearinterval, keep scope in mind