Add +1 min to countdown timer - With timer already finished or in progress

Asked

Viewed 785 times

0

Each time I click a button, I want to add +1 min to the timer.

<Button
    onPress={this.somarUmMinuto}
    title="Somar 1 minuto"
    color="#007FFF"
    accessibilityLabel="Somar 1 minuto"
/>

Code used to set the countdown:

iniciarCronometro() {
    var intervalId = setInterval(this.somarUmSegundo, 1000);
    this.setState({
      intervalId: intervalId,
    });
}   

somarUmSegundo() {
    let segundos = this.state.contadorSegundo;
    let minutos = this.state.contadorMinuto;

    minutos == 1 || segundos > 0;
    if (segundos == 0) {
      segundos = 59;
      minutos = minutos - 1;
    } else {
      segundos = segundos - 1;
    }

    this.setState({
      contadorSegundo: segundos,
      contadorMinuto: minutos,
    });
}

I need to increment the event to add +1 min to the timer, either already finished or in progress in the count.

Countdown timer lasts 1 minute.

  • See if this code helps: http://jsfiddle.net/alnitak/aBWce/

  • You have already separated the minutes and seconds (counterNumber and counterNumber), I do not understand what is really the problem. Don’t just go into the Inuto counter and increment?

  • @Jrd tried to directly increment the minute counter, but error. I used code Let sum = (this.counterMinuto +1);

  • @Philipesaid Your Philipesaid counter is in the state of the application, so to access it you need to use this.state. In place of this code you presented try to add Let = this.state.counterMinuto + 1

  • @Jrd Keeps giving error ( Undefined is not an Object ( evaluating'this.state.counterMinuto') ).

  • @Philipesaid You’re probably losing context... bind your method so you can access the state through this. Your onPress would look like this onPress={this.somarUmMinuto.bind(this)}

  • @Jrd The error no longer happens. However, it does not execute the value increment.

  • @Philipesaid Please update your code there to see what happens, these comments are already getting too long

Show 3 more comments

1 answer

1


First you need to bind to the onPress button so that it is possible to access the state of the application, it would be like this:

onPress={this.somarUmMinuto.bind(this)}

Then you need to create the function for this increment:

somarUmMinuto(){
    this.setState({
        contadorMinuto: this.state.contadorMinuto + 1,
    });
}
  • My job was completely wrong. Thank you

Browser other questions tagged

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