Ways to dynamically adjust the interval using an Observable pipe

One way to approach this is by updating the intervalValue through a button click event. This value is dynamic and changes frequently.

intervalValue: number; 
intervalValue: Observable<number>; // I also attempted using this method, but the interval requires a number input

  ngOnInit() {

    interval(intervalValue)
    .pipe( 
              debounceTime(150),
              distinctUntilChanged(),
              tap(() => { 
                    ... 
                }), 
              startWith({}),
        switchMap(() => { 
         ... 
        }),
        map(data => {
          ... 
          return data.content;
        }),
        catchError(() => {
         ... 
        })
        ).subscribe(data => this.data = data);
}                                                       

The issue arises when the intervalValue changes while the interval remains at its initial value. Is it possible to synchronize the interval with the changing intervalValue?

Answer №1

Beginning with an Intervalue stream is crucial.

const intervalValue=of(Math.random()*10000)
intervalValue
    .pipe( 
              switchMap(time=>interval(time))
              debounceTime(150),
              distinctUntilChanged(),
              tap(() => { 
                    ... 
                }), 
              startWith({}),
        switchMap(() => { 
         ... 
        }),
        map(data => {
          ... 
          return data.content;
        }),
        catchError(() => {
         ... 
        })
        ).subscribe(data => this.data = data);

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

Setting up Angular-CLI on a Windows 10 system

I encountered some challenges while trying to install angular-cli on my Windows 10 system. The issues revolved around Python dependencies and node-gyp, manifesting in error messages similar to the following: ><a href="/cdn-cgi/l/email-protection" ...

What could be causing the inner array typescript to be inaccessible in an Angular 5 application?

Below are the JSON definitions that I am working with: export class Company { name: string; trips : Trip[] = []; } export class Trip{ id: number; name: string; } Within the component, there is a method that contains the ...

Tips for concealing material input

In my usual practice, when I need a form field to be part of the submission but not visible, I typically use <input type="hidden" /> However, when working with matInput, the option for a type of hidden is not available. While I could apply display: ...

TypeScript Redux actions not returning expected type

Basically, I am attempting to assign types to a group of functions and then match them in my Redux reducer. Here are the functions I have defined in actions.ts: export const SET_CART_ITEMS = "SET_CART_ITEMS"; export const SET_CART_TOTALS = "SET_CART_TOTA ...

Acquiring a second access token in Java for the Graph API using an OIDC-compliant token can be achieved through the OBO flow method

Utilizing the angular-oauth2-oidc library within Angular allows me to login through the PKCE Authorization Flow, followed by passing the token to my backend in order to secure my custom API. The Spring boot backend functions as the oauth2 Resource Server ...

Finding a solution to the error message "CORS policy: Response to preflight request doesn't pass access control check" without resorting to browser plugins

While I realize this question may have been asked before and is considered a duplicate, I have tried the suggested solutions without success. So please take a moment to review my question and offer some assistance. My goal is to call a GET method at the U ...

Using Angular styleUrls directory over individual files

https://i.stack.imgur.com/vg7Ot.png Is there a way to dynamically include all stylesheets from a specific directory instead of hardcoding each URL in styleUrls in Angular? For example, something like: referencing all stylesheets from the './styles/&a ...

Create an array filled with multiple arrays containing objects

To achieve the desired array of array of objects structure, I need to populate the data like this: let dataObj = [ [ { content: "test1"}, { content: "test2"}, { content: "test3"} ], [ ...

Ways to convert a string into a Date object without using moment.js

I am facing a challenge with 2 dates that are stored in the following format: "04.12.2019, 09:35" // Today "05.12.2019, 12:50" // Another date I need to compare these dates to determine if they have passed or are yet to come. My initial approach was to ...

Is there a way to apply the same technique to a dynamic select option in Angular?

My array is dynamic and has an onChange method in the select option. The issue arises when I add a new array and select the new option, as it causes the first array to reset. Here's a snippet of my array structure: <ng-container formGroupName=&qu ...

Tips on conditioning a query to reject fields that do not exist

My controller has a query that is working properly, but it also executes when receiving parameters that do not exist. I attempted to handle this by using HTTP status response codes, but the issue persists as it creates an empty array when a URL is manuall ...

Using a template reference variable as an @Input property for another component

Version 5.0.1 of Angular In one of my components I have the following template: <div #content>some content</div> <some-component [content]="content"></some-component> I am trying to pass the reference of the #content variable to ...

Issues with expanding all nodes in the Angular Treeview function by Nick Perkins in London are causing difficulties

Currently utilizing the angular treeview project found here: https://github.com/nickperkinslondon/angular-bootstrap-nav-tree After examining the functionality, it seems that this treeview is lacking search capabilities. To address this limitation, I deci ...

Developing Your Own Local Variable in Angular with Custom Structural Directive ngForIn

I am hoping for a clear understanding of this situation. To address the issue, I developed a custom ngForIn directive to extract the keys from an object. It functions correctly with the code provided below: import {Directive, Input, OnChanges, SimpleChan ...

Converting an array into an object by using a shared property in each element of the array as the key

I have an item that looks like this: const obj = [ { link: "/home", title: "Home1" }, { link: "/about", title: "About2" }, { link: "/contact", title: "Contact1" } ] as const and I want to p ...

Convert an enum value to a String representation

I need assistance with converting my enum value to a string format export enum Roles { manager = "manager", user = "user" } console.log(" Roles.manager: ", Roles[Roles.manager]); The following is displayed in the console: Roles.manager: manage ...

What is the best way to incorporate Blob into Typescript?

I am facing an issue while trying to use FileSaver to save a file in Blob format within my TypeScript application. When I attempted to import the Blob module using: import * as Blob from "blob"; An error was thrown: Could not find a declaration file fo ...

Utilizing Dynamic Variables in Angular 4 Templates

Hey everyone, I recently created an Angular 4 app and developed a component called Contentholder. The Contentholder consists of HTML & CSS to display the content holder that I am utilizing. When I include <content-holder></content-holder>, ...

Distributing a library of components using Vite, Vue 3, and Typescript to npm

My current challenge involves publishing a Vue 3 component library with Vite, all written in Typescript. The issue I'm facing is that the type definitions are not being included in the package when imported into another project. Upon importing the co ...

Warning in Typescript: potential undefined access detected when strict mode is enabled

When using Typescript with "strict": true in the tsconfig.json, a common issue arises where warnings are not triggered for potentially undefined properties, as shown by this code snippet: let x: any = { test: false } let y = x.asdf // no warning issued ...