Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to restart the timer? #42

Open
akhilkurnool opened this issue Nov 21, 2017 · 10 comments
Open

How to restart the timer? #42

akhilkurnool opened this issue Nov 21, 2017 · 10 comments

Comments

@akhilkurnool
Copy link

How to restart on setState or any other condition or props change, I have tried to re-render the whole component using forceUpdate() but it doesn't seem to work.
Using this clock for OTP countdown, need to reset when the user resend OTP.

@akhilkurnool
Copy link
Author

One way I found was using history to redirect to same page.

@sjmp
Copy link

sjmp commented Dec 21, 2017

This is still an open issue! The inability to restart this component is a real pain.

@pughpugh
Copy link
Owner

No need to be quite so dramatic @sjmp . What action are you proposing would reset the timer?

I've not looked at this in a while, but I believe if the seconds prop changes, the timer will restart. It maybe that there is a bug around this that needs fixing (#45) and it might not be of use if you need to reset it to the same time.

If I find some time over the xmas holidays I will take a look.

@zaidchauhan
Copy link
Contributor

zaidchauhan commented Dec 21, 2017

@sjmp @pughpugh Setting the same time again will not work. You can do something like this

Initial state from seconds received in props or fixed. You may also use default props instead ternary operator

constructor(props){
    super(props);

    this.state = {
      seconds: this.props.seconds ? this.props.seconds : 60,
    };
  }

Set state by adding fraction.

this.setState({
     seconds: this.getNewSeconds()
 })
getNewSeconds = () => {
    //Note: This library only restart timer when we supply different seconds
    if(this.props.seconds !== this.state.seconds) {
      return this.props.seconds;
    }else {
      return this.props.seconds + 0.0000001;
    }

@pughpugh
Copy link
Owner

#45 has been fixed as well a couple of other related bugs. I'm not convinced this fully meets the requirements of resetting the timer, but might at least support a workaround for some people.

@miso2018
Copy link

miso2018 commented Feb 10, 2018

Seems like a badly needed feature.

You'll have to find a way to call the following internally:

        this._clearBackground()
        this._seconds = this.props.seconds
        this._stopTimer()
        this._setupTimer()

I needed a timer that automatically reset on complete:

class ResettableTimer extends ReactCountdownClock {
  constructor(props){
    super(props)
    this._handleComplete = () => {
        // Do what you need
        this._clearBackground()
        this._seconds = this.props.seconds
        this._stopTimer()
        this._setupTimer()
    }
  }
}

@VilliGunn
Copy link

VilliGunn commented Aug 18, 2018

Might not be the best approach but here is what I did to force the component to re-render and therefore restart.

import React, { Component } from 'react'
import CountDown from 'react-countdown-clock'
class CountDownTest extends Component {
  constructor() {
    super()
    this.state = {
      completions: 0
    }
  }

  onComplete = () => {
    this.setState(
      {
        completions: this.state.completions + 1
      },
      () => console.log('completions', this.state.completions)
    )
  }

  render() {
    return (
      <div>
        <CountDown
          key={this.state.completions}
          seconds={15}
          color="#000"
          alpha={0.9}
          size={300}
          onComplete={this.onComplete}
        />
      </div>
    )
  }
}

export default CountDownTest

this.state.completions updates every time the countdown is completed and by using a state property that is always changing as the value for the key attribute, forces the component to re-render.

@Jeremy-coding
Copy link

@VilliGunn Great solution, I'd even suggest adding it in the Readme as part of an FAQ section if there is no progress on implementing this feature into the base component.

@epozsh
Copy link

epozsh commented Aug 21, 2019

@pughpugh
What if you will add new prop countDownStartedAt
and
on

componentDidUpdate(prevProps) {
    if (prevProps.seconds !== this.props.seconds || prevProps.countDownStartedAt!== this.props.countDownStartedAt) {
      this._seconds = this.props.seconds;
      this._stopTimer();
      this._setupTimer();
    }

and the only thing we have to do is change countDownStartedAt prop to restart it.

@timgreen
Copy link

Late to the party, but note for the people are still seeking for simple solution.

Like https://www.npmjs.com/package/react-countdown#key mentioned, just set a new key to reset the component to restart the countdown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants