2

I am working in an Angular 4 application ,In this I need to call a API in every 15 minutes interval.I read some stackoverflow post but I couldn't get what I exactly looking for .

This is what I have tried so far.

 UPDATE_USER_SESSION() {

        this.publicIp.v4().then(ip => {
            this.END_USER_SESSION(ip)
        })

        this.publicIp.v4().then(ip => {
            this.INSERT_USER_SESSION(ip)
        })
    }

I have this method out side of my ngOnInit .and I want call this method every 15 minutes interval.

Inside ngOnInit I have the following

import 'rxjs/add/observable/interval';

    this.call = Observable.interval(10000)
                .subscribe((val) => { this.UPDATE_USER_SESSION() });

Can any one guide me to solve this .

Nikson
  • 900
  • 2
  • 21
  • 51
  • Have you tried to apply timeout or some time out for each 15 minutes ? to call it this.sub = Observable.interval(10000) .subscribe((val) => { console.log('called'); } and for stopping this.sub.unsubscribe(); take help from https://stackoverflow.com/questions/46096587/call-a-function-every-10-seconds-angular2 – Make May 21 '18 at 05:29
  • @Make I have tried this https://stackoverflow.com/questions/36133430/how-to-call-function-every-2-mins-in-angular2 – Nikson May 21 '18 at 05:42
  • I doubt you are making typo error please check have you written ngOnInt or ngOnInit, cross verify it. – Make May 21 '18 at 06:09
  • it's a spell mistake @Make – Nikson May 21 '18 at 06:20

2 Answers2

1

Saw your comment on my answer to this. I think it's essentially the same thing (only my examples shows an API being hit every 10 seconds instead of 15 minutes (15*60*1000= 900000).

Some things to take into consideration:

Hard to tell from your code provided what all is going on, but normally, your interaction with an API would be contained in an Angular Service, to encapsulate your interaction with that API. (You should be able to see an example of this in the solution link above.) I would recommend creating a new service to replace your END_USER_SESSION and INSERT_USER_SESSION functions.

That Angular service should probably look like:

@Injectable()
export UserSessionService {
  constructor(private http: HttpClient) {}

  public endUserSession(ip: string): Observable<void> {
    // use the HttpClient to hit the API with the given IP
  }

  public insertUserSession(ip: string): Observable<void> {
    // use the HttpClient to hit the API with the given IP
  }
}

Then your component code should be something like:

@Component({
  //...
})
export class YourComponent implements OnInit, OnDestroy {
  private alive: boolean;
  private publicIp = require('public-ip');

  constructor(private userSessionService: UserSessionService){}

  ngOnInit(){
    this.alive = true;
    TimerObservable.create(0, 900000)
      .pipe(
        takeWhile(() => this.alive)
      )
      .subscribe(() => {
        this.publicIp.v4().then(ip => {
          this.userSessionService.endUserSession(ip).subscribe(() => {
            this.userSessionService.insertUserSession(ip).subscribe(() => {
              // do nothing
            });
          });
        });
      });
  }

  ngOnDestroy(){
    this.alive = false;
  }
}

Note the nesting of the insertUserSession call inside of the subscribe() of endUserSession. This will make your insertUserSession call occur after your endUserSession call (in your code above, there's a race condition for which of the two will occur first).

ZackDeRose
  • 2,146
  • 1
  • 16
  • 28
0

I think you can use Observable for this

need to import import 'rxjs/add/observable/interval';

and use like

this.obs = Observable.interval(10000)
    .subscribe((val) => { console.log('called'); }
Hrishikesh Kale
  • 6,140
  • 4
  • 18
  • 27