Yuval Segev
Yuval Segev

Reputation: 73

Create a constantly running method

I would like to create a method that monitors the current time using momentjs and will execute this.setState() in a specific time, in react native.

I had no problems getting the current time, the problem is, that i need some kind of a method that will constantly run in the background and execute the task.

Something like angular's $scope.watch for you guys that are familiar with angular.

Upvotes: 2

Views: 83

Answers (2)

Piyush.kapoor
Piyush.kapoor

Reputation: 6803

use setInterval

componentDidMount() {
  let intervalId = setInterval(() => {
     this.setTime({
        time: //moment time
    }
});
}

Upvotes: 0

Alexandr Lazarev
Alexandr Lazarev

Reputation: 12882

You can run an interval after the component is mounted:

componentDidMount() {
  const interval = setInterval(() => {
    //your logic here
    this.setState({})
  }, 300)
}

Upvotes: 2

Related Questions