Search code examples
angularionic4

How can i get api response without refreshing the current page


I am using the below API to get current chats from my database , the thing is the api works well, but issue is i have to refresh my page each time to see new chats, so i dont want that, any suggestions on how to get new chats without refresh, see my code below

getChatsList() {
    this.api.post_private('v1/chats/getById', { room_id: this.roomId 
      }).then((data: any) => {
      console.log(data);
      if (data && data.status && data.status === 200 && data.data.length) {
        this.messageList = data.data;
      }
    }, error => {
      console.log(error);
    }).catch(error => {
      console.log(error);
    });
  }

Solution

  • function getChatsList() {
    this.api.post_private('v1/chats/getById', { room_id: this.roomId 
      }).then((data: any) => {
      console.log(data);
      if (data && data.status && data.status === 200 && data.data.length) {
        this.messageList = data.data;
      }
    }, error => {
      console.log(error);
    }).catch(error => {
      console.log(error);
    });
    }
    setInterval(getChatsList, 1000);
    

    //Use this code, without reload current page.