0

Ok so I'm making an idle game, and so that you don't have to wait every second to get ozolith(the currency in the game), I made a variable called ops(ozolith per second) and did a setInterval() run in a way that if the person has 2 ops, it would run every .5 seconds. This works most times but sometimes it goes nuts and decides it goes as fast as if the player had 1000 ops so it runs every 1 miliseconds. Here's the code that's the source of the problem to help you find an answer to this.

ops = Math.floor(((reactors*10)+mines)*((alientransbought*colplanets)+1))

if(ops>=1){
     setInterval(production,Math.floor(1000/ops)) //Production is just where it gives the ozolith and is not 
     the root of the problem
}
  • 1
    what's the value of `ops` when "it goes nuts" – Jaromanda X Jan 27 '20 at 03:15
  • I tested some scenarios of your function and works. Are you sure you're not creating multiple `setInterval()`'s ? – MarAvFe Jan 27 '20 at 03:26
  • @MarAvFe I'm sure because every time before I put a clearInterval with the ID –  Jan 27 '20 at 03:28
  • @JaromandaX It's what it should be, it starts at 1 though –  Jan 27 '20 at 03:29
  • 1
    In games you don't rely on timer precision, instead you measure time between ticks and update the state of the world correspondingly. – zerkms Jan 27 '20 at 03:43
  • Are you sure you don't set multiple intervals simultaneously? – zerkms Jan 27 '20 at 03:52
  • @zerkms I'm not sure I get what you mean. How do you measure time between ticks? –  Jan 27 '20 at 04:09
  • 1
    `It's what it should be` - and what is that? – Jaromanda X Jan 27 '20 at 04:20
  • When ops equals 1000, your function will always try to run every ms because 1000/1000 is 1. To get the effect you want you'll need to scale the numbers. – Richard Barker Jan 27 '20 at 04:33
  • I know that it'll run every milisecond when ops is at 1000, and it's what I want. my question is why did it work as if it was at 1000 when it was at 1, but I found out why. –  Jan 27 '20 at 04:41

2 Answers2

0

Web browsers, like all applications, take turns for a piece of CPU time, and the time they have to wait will vary, depending on the load. This is what causes the latency in asynchronous timers — a 200ms timer may actually take 202ms, or 204, and this will gradually send the stopwatch out of time.

Take a look at this article as cited in this answer. It explains how to create a "high resolution timer".

MarAvFe
  • 468
  • 4
  • 15
  • thank you for this, but it is not what I need. When I say it goes crazy, I mean it goes crazy fast. I hope this can help better understand my problem –  Jan 27 '20 at 03:44
0

I found something that could help. Usually, the second part of the ops variable: ((alientransbought*colplanets)+1)) it usually stays at 0, unless you buy it, which is later in the game. I've found that it usually only appears if you buy this first so I think I'm fine. Thanks for the help though!

  • "It's what it should be" --- As always, the problem was with _assuming_ thing instead of measuring it. – zerkms Jan 27 '20 at 05:45
  • I'm not assuming because I check when it goes nuts –  Jan 29 '20 at 17:39
  • "because I check", "I've found that it usually only appears", "It's what it should be" --- I find these 3 to be contradictory. But whatever. – zerkms Jan 29 '20 at 20:46
  • I found the root of the problem, I had somewhere else I called the function and it messed up everything. All is well –  Jan 29 '20 at 23:29