Call method every so often ionic wordpress


I bring news from a wordpress, but I want to be able to update the news every so often, and it would be calling the builder who brings the news ... I show the code.

import { Injectable } from '@angular/core';
export const WORDPRESS_URL = '';
export const WORDPRESS_REST_API_URL = WORDPRESS_URL + 'wp-json/wp/v2/posts?categories=817';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/forkJoin';
import { Http } from '@angular/http';

export class WordpressProvider {
  constructor(public http: Http) {
  getRecentPosts(page:number = 1){
    return this.http.get(
    .map(res => res.json());
asked by Aldo Gabriel Martinez 25.05.2018 в 03:40

1 answer


There are several things that catch my eye: It seems that you do not have a service , but the controller calls the server directly to get the data. It is not a good practice.

On the other hand, if you are working with Ionic 3, the Angular version should be at least 4.3, so you should be able to use HttpClient instead of Http to make the AJAX calls.

To finish, if you want to create a call that is made repeatedly, you can do something like:

import { timer } from 'rxjs/observable/timer';

const TIME_INTERVAL = 30000; // milliseconds

// ...  en el método:
return timer(0, this.TIME_INTERVAL).switchMap(
    () => this.http.get(this.api_url ).map(res => res.json());

This code makes a GET call instantly and then repeats it every 30 seconds.

answered by 29.05.2018 / 09:47