plougsgaard / react-timeout

Component wrapper for setTimeout et al that cleans up after itself.
127 stars 17 forks source link

How to pass arguments to function in timeout #48

Closed rohitgulia closed 5 years ago

rohitgulia commented 5 years ago
  handleClickOnRoute = async(selectedRouteData) => {
    try {
      clearTimeout(this.timeOut);
      await this.props.setSelectRouteAction(selectedRouteData);
      await this.props.getStopsListAction(this.props.selectedRoute.id).then(()=>{
        this.timeOut = setTimeout(this.handleClickOnRoute, 5000, selectedRouteData);
      });

    } catch(err) {
      clearTimeout(this.timeOut);
      this.props.addMessageCreator(err.message, WARNING);
    }
  }

When function runs next time it passes null argument. I am not able to figure out how to pass argument to it. Normally in timeout we have third parameter to pass as argument, but that doesn't work here i guess.

plougsgaard commented 5 years ago

To be honest I did not know that was the official signature of setTimeout. I'll have to look into that.

In the meantime you could just wrap your function in an anonymous function to get the same result:

this.timeOut = setTimeout(() => this.handleClickOnRoute(selectedRouteData), 5000);

plougsgaard commented 5 years ago

I'll cut a minor release with this soon.

rohitgulia commented 5 years ago

I'll cut a minor release with this soon.

Thanks for the reply @plougsgaard