Want to calculate the time a status is not changed with setInterval

const timer = setInterval(async () => {
          const processDoc = await this.get('documentsAdapter').getDocument(res.document_id)
          this.controller.set('model.processed', processDoc) 
         //model.processed.status to get the status of current document. Status can be fetch, inprogress, review etc
          }
        }, 5000);  

This setInterval updates every 5 seconds. I want to add a variable that will store the amount of time the status was same after the interval. For instance after 3 setInterval, the status is IN_PROCESS the time should be saved in the variable, 150000 milliseconds which is 15 seconds. What I think is making a loop that will check if the two statuses (previous and current) are same it will add time+=5 at each loop and make time variable reset if the statuses are different. But I just don’t seem to get it. Any help is appreciated. Thanks.

This doesn’t have enough detail for me to give you a working solution. If you could say more about what the feature is that you’re trying to implement, you’ll get more suggestions.

It’s easy to make mistakes with this kind of asynchrony, which is why many apps use ember-concurrency to help. Start by reading the tutorial to understand what kinds of problems it solves.

I’ll assume you’re trying to run some asynchronous code periodically to keep a value updated on a component. In that case, I’d make an ember concurrency task like:

import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { task, timeout } from 'ember-concurrency';

export default class MyCompontent extends Component {
  @tracked processed;

  constructor() {
    super(...arguments);
    this.keepUpToDate.perform();
  }

  @task *keepUpToDate() {
    while (true) {
      this.processed = yield this.get('documentsAdapter').getDocument(res.document_id);
      yield timeout(5000);
    }
  }
});
1 Like