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

connect setinterval with beginsampleData() #30

Open
newAiro opened this issue May 4, 2017 · 4 comments
Open

connect setinterval with beginsampleData() #30

newAiro opened this issue May 4, 2017 · 4 comments

Comments

@newAiro
Copy link

newAiro commented May 4, 2017

hi, so i want to introduce an interval each second in the trafficFlow.jsx, and connect this interval with beginSampleData() function, @jrsquared i tried to do that in the componentDidMount() function but it did not work, nothing change and there is no call of the beginSampleData each second, i would greatly appreciate some direction.

@jrsquared
Copy link
Contributor

Can you show me exactly what you tried?

@newAiro
Copy link
Author

newAiro commented May 4, 2017

@jrsquared here the modification i did, i want to make the http call each second and update the data
thanks in advance,

import * as utils from './newformatt.js'
.....
beginSampleData () {
    this.traffic = { nodes: [], connections: [] };
    request.get('http://localhost:8080/api/v1/metrics')
      .set('Accept', 'application/json')
      .end((err, res) => {
        if (res && res.status === 200) {
          this.traffic.clientUpdateTime = Date.now();
          this.updateData(utils.newformatt(res.body));
        }
      });
  }

  componentDidMount () {
    this.checkInitialRoute();
    **this.setInterval(this.beginSampleData(),10000);**

    // Listen for changes to the stores
    filterStore.addChangeListener(this.filtersChanged);
  }

@aSqrd-eSqrd
Copy link
Contributor

@newAiro,

I had a bit of trouble with the same thing also. What I ended up having to do was the following

componentDidMount () {
    this.checkInitialRoute();

    const self = this;
    setInterval( () => { self.beginSampleData(self); }, 10000);
}

I actually got that from @jolexa's fork.

That said, I did make another incremental improvement to let the timer be cleaned up nicely. Otherwise, the timer runs forever even if you've navigated away from the React component. I created a global variable in trafficFlow.jsx that in my case I called intervalHandle, then I did the following:

componentDidMount () {
    this.checkInitialRoute();

    const self = this;
    intervalHandle = setInterval( () => { self.beginSampleData(self); }, 10000);
}

Then in componentWillUnmount

componentWillUnmount () {
    filterStore.removeChangeListener(this.filtersChanged);
    clearInterval(intervalHandle);
}

If you still have trouble I would check your request.get is address as it might just need to be relative such as /api/v1/metrics, but that likely is dependent on your base address/route/project structure...

@edperry
Copy link

edperry commented Apr 21, 2018

GREAT information, this helped me a lot!!!! Thanks

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

No branches or pull requests

4 participants