How can I manually trigger a refresh on an rxjs timer and change the parameters of the observable?

Developing the pagination for a web page displaying data updated every 2 minutes from an API, I encountered the need to implement custom pagination in order to control how the data is shown. For example, displaying 100 elements per page on page number 5 would show elements ranging from 401 to 500.

private readonly _start = new Subject<void>();
private readonly _stop = new Subject<void>();
lista : any = [];
pNum = 1;
act = timer(0, 120000);

constructor(private ListaService:ListaService) {
 this.act.subscribe(() => {
  this.ListaService.getLista(this.pNum,'usd').pipe(
    takeUntil(this._stop),
    repeatWhen(() => this._start)
  ).subscribe(resp=>{
    this.lista = resp
  });
 })
}

start(): void {
 document.body.scrollTop = 0;
 this._start.next();
}
stop(): void {
 this._stop.next();
}

Upon clicking the button to navigate to a different page, the methods stop() and start() are invoked, updating the pNum variable with the selected page number. However, upon doing so, I noticed that the data does not immediately load for the newly selected page, but rather updates after the 2-minute interval set by the timer.

I am wondering if there is a way to manually refresh the timer and update the observable parameters?

Answer №1

Sure, make sure to update your act to include a source for manual updates by doing the following:

manual$ = new Subject<void>();
act = merge(timer(0, 120000), this.manual$)

Then, whenever you require a manual update, simply call next on manual$ like this:

this.manual$.next();

UPDATE

In addition, avoid nesting subscribe calls - instead, use a flattening operator like switchMap, as shown below:

 this.act
   .pipe(switchMap(_ => this.ListaService.getLista(this.pNum,'usd')))
   .subscribe(resp => this.lista = resp);

Check out the demo on stackblitz.

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

There seems to be an issue with the function code error when calling it within the

When I attempt to run my code in this way, I encounter a compile time error stating that the expression statement is not an assignment or call... (within the else statement). What am I missing here to get it to work? I've made numerous attempts to ad ...

Accessing the jQuery Ajax success variable

I have a PHP function that returns an array with an element error containing the value 'ERR': var updatePaymentType = function(plan_pt_id, pt_id){ var error = null; var data = new Object() data["function"] = "update"; ...

Vuejs is throwing an error claiming that a property is undefined, even though the

I have created a Vue component that displays server connection data in a simple format: <template> <div class="container"> <div class="row"> <div class="col-xs-12"> <div class="page-header"> < ...

Utilize Angular 8 to dynamically populate Input values with data pulled from an API

I need help with setting the input value dynamically using data from my API. Once I click send, I want it to be saved in the database. However, I am struggling to dynamically populate the input field. Can someone guide me on the right approach to achieve t ...

Single-use condition for checkbox functionality in JavaScript

As a newcomer to javascript programming, I am struggling to make a checkbox display two radio buttons only when it is unchecked, even after researching solutions. Here is my current code snippet: <span><strong>Not Available</strong></ ...

The Power of AngularJS Directives in Harnessing jQuery DOM Event Bindings

I have developed a new directive and have some doubts about the binding syntax like element.bind("click", function(){}). Every time the link function of the directive is called, it creates a duplicate binding. What is the best Angular approach to handle th ...

The specified container is not a valid DOM element. (Utilizing Ant Design carousel component in React.js)

Here is a snippet of my code where I am having an issue with my carousel. It seems to be giving me an error message "Target container is not a DOM element" when I try to run the server. I have divided my code into two parts - the first part containing the ...

Converting HTML to PDF using JavaScript

My goal is to create an HTML page and then print and download it locally as a PDF. However, when I try to generate the PDF, it cuts off the last 3 to 4 lines of the page. I want to create multiple PDF pages in A4 size with the same styling as the HTML pa ...

Incorporate a collection of product titles along with their short forms in JavaScript/jQuery

Seeking guidance as a newcomer to JS. I have encountered the need for two different views in an application I am working on - one displaying full product names and the other showing only their abbreviations. Instead of hard-coding this information, I wish ...

The workings of the toString() function within Objects

Recently while delving into a book on Js, I stumbled upon the intriguing topic of Object to primitive conversion. The author made an interesting point in the book: For historical reasons, if toString or valueOf returns an object, there’s no error, but ...

What is the process for consolidating a TypeScript web application with node modules into a single JavaScript file using the command line

How can I efficiently set up a TypeScript web application with all node modules compiled into one JavaScript file for use in HTML? If my project structure looks like this: ├── node_modules │ └── mathjs │ └── (dependencies, etc.) ...

The error message "npm ERR! enoent" indicates that npm is unable to locate a specific file

How do I troubleshoot this issue? After attempting the master version, I encountered a similar error. My operating system is OSX Yosemite: bash-3.2$ yo meanjs You're utilizing the official MEAN.JS generator. ? Which version of mean.js would you like ...

Executing a get request in Backbone without using the Option parameter by implementing the beforeSend method

After gathering insights from the responses to this and this queries, I have formulated the code below for a GET request: var setHeader = function (xhr) { xhr.setRequestHeader("Authorization", "Basic " + btoa($rootScope.login.Gebruikersnaam + ":" + $r ...

The Raycast function seems to be failing to detect the GlTF file in Three.js

After creating a binary gltf file in Blender and animating it with Mixamo, I am encountering an issue where it is not being detected on raycast. Despite trying numerous tutorials and solutions, I have been unable to resolve the issue. const loader = new GL ...

JavaScript file encountering a Typescript issue with a property defined in a subclass

Currently, I am utilizing Typescript to validate my Javascript files. One issue I have encountered is that when I inherit from a class, Typescript does not recognize the types of the properties in the parent class. I am unsure if I am overlooking something ...

Can AngularJS Filters be used to convert a number into a string, but not the other way around?

After researching Angular JS filters, I discovered that the number filter is used to format a number as a string. However, there doesn't seem to be a built-in filter for converting a string to a number. In an attempt to solve this issue, here is so ...

Sending information from Vue to Express for processing

Is there a way to pass data from Vue to an express server? For example, in the scenario below, I am looking to send the data logged in my Vue function to the "id" variable on the server side. expressApp.post('/delete' , function (request, respons ...

Please explain the purpose and scope of a JavaScript document

let page = "temp"; function showPage() { alert(page); // temp or HTML Page } When I tried it, the result was "HTML Page", which makes sense. Is 'page' an object, reserved word, or a constant? If 'page' is an object, shouldn' ...

The router is unable to direct when an item is clicked

I am encountering an issue with my routing setup - when I click on an item in the list-item component, it does not successfully route to the detail-component. Here is a glimpse of my source code: product-list.component.html: <h1>Product List Compon ...

Creating a dynamic background image for the ul/li div element

As a newcomer to website development, I am currently exploring how to dynamically set images in the ul/li elements within a div. Below is a snippet of the HTML code I have been working on: <div class="results"> <ul class="row"> < ...