Javascript clock increment for game

Asked

Viewed 59 times

0

I am creating a JS team to count the time it will take the user to complete the task. I am using setInterval() to run the team but I’m not able to increase 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());

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

1 answer

1


The problem is the attribution you are doing. No 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 anointing + of one value in another:

a =+ b
a = + (b)

When using the += is using an assignment operator, which will add one more in the value that has:

a += b
a = a + b

You can see the resulting values difference 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);

  • Bro thanks for real, I fix here and it worked now yes this incrementing the minutes. Thank you really, God bless you man.

  • I hadn’t really realized the assignment of =+ thanks man.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.