I cant to figure out how my timer works

html-css

#1

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


#2

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


#3

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


#4

this i will put a codepen link now


#5

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


#6

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.


#7

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


#8

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


#9

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


#10

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


#11

clearInterval is clearly what you need to stop the counter.


#12

Did you figure it out?


#13

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…


#14

check documentation:

https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval

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