What is the best way to retrieve information from the server at regular intervals?

I am utilizing Angular 6 and attempting to retrieve JSON data from the server at a 5-second interval.

Check out this JSFIDDLE

This is my service code:

getExample(): Observable<any> {
    console.log('getExample');
    return Observable
      .timer(0, 5000)
      .flatMap(() => this.httpClient.get('https://jsonplaceholder.typicode.com/posts'))
      .map((r) => {
        return r;
      });
  }

I would appreciate any assistance in fixing this code.

PS: A simpler service worked without any issues

Answer №1

RxJS 6 now utilizes pipeable operators. Additionally, the timer function now has imports from a different path. It is recommended to directly import timer (or

interval</code) and apply <code>.pipe
with the necessary operators. Lastly, replace flatMap with mergeMap.

import { interval } from 'rxjs';
import { mergeMap, map } from 'rxjs/operators';

return interval(5000).pipe(
  mergeMap(() => this.httpClient.get('https://jsonplaceholder.typicode.com/posts')),
  map((r) => {
    console.log(r);
    return r;
  })
);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am looking for a custom script for a splash page that will automatically redirect users to one of three designated pages based on the information stored

As a programmer, I'm well-versed in coding but lack knowledge about creating and utilizing cookies. If anyone could provide guidance on this matter, it would be highly appreciated. I believe I require two concise scripts for this task. 1st Script: T ...

Perform the script only when a click event occurs

I need help with a script that will only refresh the page when a specific link is clicked. Currently, the script is running on page load instead of waiting for the click event. As someone who is not familiar with JavaScript, I am struggling to figure this ...

Is it possible for me to generate values using PHP that can be easily read by JavaScript?

I'm currently building a website and I am facing some challenges when trying to incorporate JavaScript for real-time calculations. Here are my issues: Is there a more efficient way to avoid manually typing out the code for each level up to 90, lik ...

Issues with loading AddMarker on initial launch in Ionic 2

Can someone help me figure out what's causing the issue in my code? When I try to load a google map in my ionic 2 app, the marker doesn't show up the first time. It only appears when I reload the map for the second time or later. I also need ass ...

What is the best way to showcase a local image in the console using nwjs?

I am currently developing a desktop application using NW.js (node-webkit). In relation to this topic google chrome console, print image, I am attempting to display an image in the console. Following the suggestion from the aforementioned topic, the follo ...

Issue concerning the relative path of RequireJS optimizer

Currently, I am attempting to implement the require optimizer's browser example. My folder structure is set up as follows, with the "r.js" and "build.html" files located at the same level as the "js" folder. js lib | a.js | b.js | ...

Rotation Ensuring a Seamless Transition to a Specific Angle

I'm currently trying to figure out how to rotate an object at a speed of 160 degrees per second, gradually slowing down until it comes to a stop at a specific angle. For instance, if the target angle is set to 30 degrees, the object should spin quickl ...

Search through an array of Ajax responses in Vue.js as the user types

I have encountered an issue where I am attempting to filter a list based on user input in a text box that is populated via an Ajax call. It seems like the filtering process is being applied before the Ajax call has completed. Here is the structure of the ...

The specified property cannot be found in the type 'IntrinsicAttributes & ...'

I'm currently working on adding a custom prop to a custom styled-component: interface Props { image?: string; title?: string; subtitle?: string; background?: string; } export function CardWide({ image, title, subtitle, background }: Props) ...

Modifying ng-model after file selection in a directive

I have a unique directive that is able to detect changes in an input field of type file. When this change occurs, my goal is to then locate another input field and modify its ng-model. The issue I am facing is that I am unable to trigger the model change s ...

What is the method for binding context on a click event in Vue?

Can't access context function on click in Vue Example HTML with click function: <li v-on:click="itemClick($event, this)"></li> My Vue.js code snippet: var app = new Vue({ el: '#mainApp', methods: { ite ...

Is there a way to delete information that is saved in local storage?

When I use console.log(localStorage.getItem("cartCache")), the output is as follows: {"dataCache":[{"id":20,"quantity":1,"total":100000,"request_date":"27-08-2017 20:31:00"},{"id":53,"quantity":1,"total":200000,"request_date":"27-08-2017 20:38:00"}]} I a ...

jQuery and CSS3 for importing and customizing text files

I successfully customized the file input utilizing jQuery and CSS3. However, there is one aspect I couldn't quite figure out. After the user selects an image or file, I want to display the selected file's text at the very bottom of the text like ...

Tips for narrowing the spacing between bars in a bar graph

Is there a way to decrease the space between bars without increasing the width in an example? I only want to reduce the space but keep the width the same. How can this be achieved? https://i.sstatic.net/cRqaQ.png options: { responsive: false, ...

Obtain the value of the background image's URL

Is there a way to extract the value of the background-image URL that is set directly in the element tag using inline styling? <a style="background-image: url(https:// ....)"></a> I attempted to retrieve this information using var url = $(thi ...

Combining Various Data Types in a Flexible List

I'm looking for a way to dynamically add rows to a table. Right now, I have the input type on the server (whether it's int, bool, string, etc), but I want to know how to implement a field accept combobox. Here is the code in cshtml: <tr ng-r ...

Embedding the view in a partial with Handlebars.js: A comprehensive guide

It might seem a bit perplexing, but allow me to explain the issue. In my NodeJs web application, I am utilizing express-handlebars. To collect user input through forms, let's consider having two distinct forms in two different views: 1) Login and 2) ...

Addressing the spacing and alignment issues within the progress bar

I need some help with my Angular app. I am currently working on creating a progress bar, but I am facing some issues with the alignment of the bars. Here is the code snippet that I have used: CSS: .progressbar { position: relative; height: 56px; mar ...

"The `ngClass` directive allows for applying classes as an `object` rather than just a simple `class` value

When applying the class name like this: <tr *ngIf="crud.isCreate" [ngClass]="{'create' : crud?.isCreate}"> The class name is not being added correctly. In HTML, it appears as: <tr _ngcontent-yql-c9="" ng-reflect-ng-class="[object Obje ...

Saving a PHP form with multiple entries automatically and storing it in a mysqli database using Ajax

My form includes multiple tabs, each containing various items such as textboxes, radio buttons, and drop-down boxes. I need to save the content either after 15 seconds of idle time or when the user clicks on the submit button. All tab content will be saved ...