Is there a way to continuously refresh a variable every minute and utilize it as a criterion?

I need to automatically update a variable maxReq, which keeps track of the number of requests sent. At the beginning of the application, every 60 seconds, the variable should be reset to 100.

getData(URL){
  if(this.maxReq <= 0)
     // wait until this.maxReq is set to 100 again

  // after this.maxReq is set to 100 again return: 
  return this.http.get(URL).toPromise();
} 

To achieve this, I must monitor both the time and the variable maxReq. The timer should run independently of everything else, counting down from 60 to 0 from the start of the application to the end. When the timer reaches 0, maxReq needs to be updated and the timer reset to 60 seconds - repeating this process every 60 seconds until the application is closed.

What would be the most effective way to accomplish this task?

Answer №1

let countdown = 60;
let startTime = new Date();
setInterval(()=> {
   const secondsPassed = (new Date() - startTime) / 1000;
   countdown = 60 - secondsPassed;
   if (secondsPassed >= 60) {
      startTime = new Date();
      countdown = 60;
      maxRequests = 100;
   }
}, 1000);

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

Implementing routing for page navigation within an angular tree structure

Can someone assist me with integrating a route into an angular tree structure? Here is the HTML code snippet: <mat-tree [dataSource]="dataSource" class="tree-container" [treeControl]="treeControl"> <mat-tree-node class="btnLinks" *matTreeN ...

Ways to address the issue of duplicated names in input fields

Currently, I am utilizing a React Hook Form hook known as useFieldsArray. This hook is responsible for rendering an array of fields, each containing an object with the data that will be transmitted via the input. One interesting feature is the ability to ...

In order to determine if components linked from anchor elements are visible on the screen in Next.js, a thorough examination of the components

Currently, I am in the process of developing my own single-page website using Next.js and Typescript. The site consists of two sections: one (component 1) displaying my name and three anchor elements with a 'sticky' setting for easy navigation, a ...

The console in Cypress does not display the contents of an array

Currently, I am attempting to store the titles of an iframe header in an array and then display the array with its elements in the console. Although the elements are being added successfully, there is a problem where the console shows "Array[9]" instead of ...

Navigating route parameters in Angular Universal with Java

I am currently developing a web application using Angular 5 with Server Side Rendering utilizing Angular Universal for Java. The project repository can be found here. One of the challenges I am facing is with a parameterized route defined in Angular as /pe ...

Verify whether the mat-dialog is currently displayed

Purpose: To trigger a dialog on page load only if it hasn't already been opened. The dialog component is separate from the current page. Issue: The dialog is opening twice. I attempted to troubleshoot by referencing StackOverflow articles like Angul ...

Is it possible to enforce a certain set of parameters without including mandatory alias names?

My inquiry pertains to handling required parameters when an alias is satisfied, which may seem complex initially. To illustrate this concept, let's consider a practical scenario. If we refer to the Bing Maps API - REST documentation for "Common Param ...

Attempting to start the server with http-server resulted in a TypeError indicating that Readable.from is not a valid function available

I am currently integrating PWA into my new Angular project. C:\Users\alan_yu\angular-pwa>http-server -p 8080 -c-1 dist/angular-pwa Initializing http-server to serve the files in dist/angular-pwa http-server version: 14.0.0 http-server ...

What are the appropriate token classifications for Dependency Injection (DI)?

Back in the days of Angular 1, providers only accepted strings as tokens. However, with the introduction of Angular 2, it seems that class tokens are now being predominantly used in examples. Take for instance: class Car {} var injector = ResolveInjector ...

Issue with Ant Design form validation

After reading through the documentation, I attempted to implement the code provided: Here is a basic example: import { Button, Form, Input } from "antd"; export default function App() { const [form] = Form.useForm(); return ( <Form f ...

Implementing a spread operator in React.js with TypeScript to add the final element only

I'm currently facing an issue with adding an element to an array in react.js this.state.chat_list.map(elem => { if ( elem.name .toLowerCase() .indexOf(this.props.searching_username.toLowerCase()) !== -1 ) { this.setState( ...

Ensuring data types for an array or rest parameter with multiple function arguments at once

I am looking for a way to store various functions that each take a single parameter, along with the argument for that parameter. So far, I have managed to implement type checking for one type of function at a time. However, I am seeking a solution that al ...

Launch an Angular 2 application in a separate tab, passing post parameters and retrieve the parameters within the Angular 2 framework

One of the requirements for my Angular 2 application is that it must be able to capture post parameters when opened in a new tab from a website. How can I achieve this functionality in Angular 2? Is there a way to capture post parameters using Angular 2? ...

Unexpected Failure in Angular 7 Compilation

My angular app was functioning perfectly, but suddenly it stopped compiling. I received the following message: ng build --prod 10% building modules 3/6 modules 3 active ...ogress\kendo-theme-default\dist\all.cssBrowserslist: caniuse-lite i ...

Modules will load after the initial rendering of the app is complete

Working on a single page application where multiple cards are displayed on the main page. Each card can be enabled or disabled - when disabled, only the title is shown, but when enabled, both the title and lazy-loaded content are displayed. By default, all ...

Different Types of Props for Custom Input Components using React Hook Form

Struggling with creating a custom FormInput component using React Hook Form and defining types for it. When calling my component, I want to maintain autocompletion on the name property like this ... <FormInput control={control} name={"name"}& ...

Error message occurs when the component containing a function child is used and results in: "Invalid JSX element, as the return type is not a valid ReactNode."

Check out this React code snippet: (codesandbox) import type { ReactNode } from 'react'; type RenderCallbackProps = { children: (o: { a: number }) => ReactNode, }; function RenderCallback({ children }: RenderCallbackProps) { return (chil ...

Top method for transforming an array into an object

What is the optimal method for transforming the following array using JavaScript: const items = [ { name: "Leon", url: "../poeple" }, { name: "Bmw", url: "../car" } ]; into this object structure: const result = ...

Limiting the number of rows in an HTML table: a comprehensive guide

Need help with displaying item names in a table with a limit of three rows. If there are more than 3 items, the 4th item should move to the second column, and if there are more than 6 items, the 7th item should go in the third column. Currently, I am using ...

Display an error popup if a server issue occurs

I'm considering implementing an Error modal to be displayed in case of a server error upon submitting a panel. I'm contemplating whether the appropriate approach would be within the catch statement? The basic code snippet I currently have is: u ...