JavaScript Clock increase for game

0

I am creating a team in JS to count the time the user will take to complete the task. I am using setInterval() to run the team but I am not able to increment to more than (1) the value of min() . This is my code:

const time = {
    min: 0,
    sec: 0,
    getSec: function(){
        this.sec++;
        if (this.sec === 59) {
            this.min=+1;
            this.sec =0;
        }else {
                if (this.sec >= 59) {
                this.sec = '00';
                this.min=+1;
            }
        }
    },
}

$sT = $('.score-panel').find('#time');
function setTime () {

    setInterval(function(){
      time.getSec();

    $sT.html(time.min + ':' + time.sec);
    },1000)
}
window.clearInterval(setTime());

if within else was an attempt to increment the minutes as the second gives 59. The window.clearInterval() is to stop the time when the user changes screen.

    
asked by anonymous 31.08.2018 / 23:02

1 answer

1

The problem is the assignment you are making. In this.min =+ 1 you are assigning the positive value of 1, so it always results in 1.

The sign of =+ is used to assign the unary + of a value to another:

a =+ b
a = + (b)

When using += you are using an assignment operator, which will add one more to the value you have:

a += b
a = a + b

You can see the resulting difference in values below:

num = 10;
num2 = -5;
num =+ num2;
console.log("Operação com '=+' retorna " + num);

num = 10;
num2 = -5;
num += num2;
console.log("Operação com '+=' retorna " + num);
    
01.09.2018 / 06:00