0

I want to decrement timeleft using an interval like this:

function timerStart(){

  let timeleft = 14;

  timer = setInterval(function(){

  timeleft -= 0.1;
  console.log(timeleft);
  
  }, 100)

};

timerStart();

The output is:

13.9

13.8

13.700000000000001

13.600000000000001

13.500000000000002

13.400000000000002

13.300000000000002

13.200000000000003

13.100000000000003

13.000000000000004

12.900000000000004

12.800000000000004

12.700000000000005

...

I don't understand why I get those extra decimals cause I simply have timeleft -= 0.1;.

The desired result is:

13.9

13.8

13.7

13.6

13.5

13.4

13.3

13.2

13.1

13.0

12.9

12.8

12.7

...

Why this happens and how to fix this?

foxer
  • 811
  • 1
  • 6
  • 16

0 Answers0