Using Rxjs interval operator for Angular 2 HTTP calls at regular intervals

While attempting to make an http call like this:

return this.http.get(this.url).map(res => res.json());

everything works as expected, with the correct response and no errors. However, when I try to make an http call using an interval (using the RxJS operator interval), an error occurs.

My code is as follows:

   return Observable.interval(1000).map(() => {
    return this.http.get(this.url).map(res => res.json());
  });

The error message reads:

ZoneAwareError {__zone_symbol__error: Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:3000/node_modules/rxjs Error: …, __zone_symbol__stack: "(SystemJS) XHR error (404 Not Found) loading http:…alhost:3000/app/home/components/home.component.js", originalErr: ZoneAwareError}

Answer №1

To extract information from a different Observable, you will need to utilize .flatMap() along with IntervalObservable:

return IntervalObservable
    .create(1000)
    .flatMap(() => {
        return this.http.get(this.url).map(res => res.json());
     });

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

Identify the CSS class for the ionic component on the webpage

Currently, I am in the process of developing an application using ionic 2 and angular 2. Within this app, I am utilizing the ionic 2 component known as ModalController. Unfortunately, I have encountered a challenge when attempting to adjust the size of th ...

Tips for turning off specific row elements within a formGroup in Angular

Hey there, I'm looking to disable a specific row that is selected through a checkbox. I've tried the code below but it's not working, so I could use some help with it. Thanks in advance! HTML file code: <form [formGroup]="myFormGroup"& ...

Display open time slots in increments of 15 minutes on Fullcalendar

Currently, I am utilizing the fullcalendar plugin with the 'agendaweek' view. My goal is to showcase the available time slots as clickable and highlight the busy ones with a red background. Handling the highlighting of busy slots is not an issue ...

Challenges with Pushing Arrays to JavaScript Class Prototype

Upon clicking a button, I have the following script that triggers: <button>Click</button> <script> var test = function(){}; test.prototype = { item:[] }; $(document).ready(function(){ $("button").on('click',fu ...

Exploring the methods of accessing $scope within an AngularJS directive

My custom directive is designed for handling form controls like input, select, and radio buttons. Each input has a default value set, and if the corresponding value exists in $scope.answers, it should be displayed in the input box. The code snippet below ...

Guide on dividing and presenting ajax information into two separate div containers

I am attempting to showcase two sets of data on separate divs using ajax. Below is the code I am utilizing for this task. Here is the ajax implementation $(function () { $(".myBtn").click(function () { var id = $(this).data("id"); ...

Create HTML div elements dynamically with JavaScript based on database information

Can javascript be used to create divs from database information? ...

Typescript -> In a React Native project, the type of 'value' is classified as 'unknown'

While working on converting a JS file to TS within my react native project, I encountered an issue that I am struggling to resolve. The problem arises when the value['flag'] is displaying an error message stating 'value' is of type &apo ...

Using PHP and jQuery to generate push notifications can result in issues with server performance

To simulate push notifications using PHP, I have implemented the following method: An AJAX call is made to a server-side script using jQuery. The script includes a for loop with a sleep function after each iteration to introduce delay. If a certain condi ...

Mastering the art of debugging a mongoose action in node.js

I am utilizing mongoose for connecting my node.js app with mongoDB. However, I am facing an issue where the database does not get updated when I create or update a model instance. How can I effectively debug and identify what goes wrong in the create or up ...

Unable to invoke a function despite using a typeof type guard

function function<T>(argument: T | (() => T)) { // @ts-expect-error return typeof argument === "function" ? argument() : argument; } Despite using the typeof type guard, I'm unable to call argument, but this issue only aris ...

Is it feasible to alter cookie data within a jQuery ajax call?

I'm currently developing a Chrome extension that enables users to capture all HTTP requests for a specific website, edit components of the request, and then send it again. My plan is to utilize jQuery's ajax function to design and dispatch the a ...

What are the steps to implement character movement in a 2D game using JavaScript?

I'm having trouble getting the image with the ID "yoshi" to move around in my 2D game document.onkeydown = (e) => { if (e.keyCode == 37) yoshi.style.left = yoshi.offsetLeft - 5 + "px"; else if (e.keyCode == 38) yoshi.style.top = yoshi.offset ...

How to insert a new list item with specific attributes using Jquery

Can anyone help me troubleshoot why I keep getting an error when trying to add a list item with attributes in my code? <div class="subway-map" data-columns="12" data-rows="10" data-cellSize="40" data-legendId="legend" data-textClass="text" data-gridN ...

API response in NodeJS encounters failure following fs.writefile operation

A new API has been developed to create a file and then request a log API after the file is written. The response will be sent back to the user based on the log API response. Here is a simplified version of the code: const express = require('express&a ...

Husky and lint-staged failing to run on Windows due to 'command not found' error

I'm facing issues with getting husky and lint-staged to function properly on my Windows 10 system. Here's how my setup looks like: .huskyrc.json { "hooks": { "pre-commit": "lint-staged" } } .lintstagedrc ( ...

Directing a controller assignment in AngularJS 1.2 via a directive

Transitioning from angularJS 1.0 to 1.2 has presented a challenge for me when it comes to assigning a controller to a directive with a distinct scope, without explicitly defining the controller in my HTML using ng-controller. Let's look at this scena ...

Do we really need TypeScript project references when transpiling with Babel in an Electron project using Webpack?

Currently, I am in the process of setting up my project configuration and have not encountered any errors so far. However, based on my understanding of the Typescript documentation... It appears that Project references are not essential when using babel-l ...

Change the position of a Div by clicking on a different Div using JQuery for custom movements

I have successfully managed to slide the div left/right based on the answers below, but I am encountering some issues with the top div. Here are the specific changes I am looking to make: 1. Make both brown lines thinner without affecting the animations. ...

Angular: Preserve the URL even when encountering a 404 page

Creating a custom 404 page in Angular 4 is something I have recently done, and I am looking for a way to preserve the incorrect URL that was input by the user. To see an example of this behavior, you can visit sites like GitHub. They show a 404 page when a ...