Replace a portion of text with a RxJS countdown timer

I am currently working on integrating a countdown timer using rxjs in my angular 12 project. Here is what I have in my typescript file:

let timeLeft$ = interval(1000).pipe(
  map(x => this.calcTimeDiff(orderCutOffTime)),
  shareReplay(1)
);

The calcTimeDiff() function will return the values for seconds, minutes, and hours;

My goal is to generate a string from these values and display it in the HTML. Essentially, I need to replace a specific word in the string with timeLeft$, which represents the countdown timer to be shown in the HTML.

For example: this.orderCutOffMessage = someString.replace('Down', timeLeft$)

orderCutOffTime = "1645567200000" // Unix timestamp in milliseconds

private calcTimeDiff(cutOffTime: any): timeComponents {
const finalCutOffTime = (cutOffTime).valueOf();
const milliSecondsInASecond = 1000;
const hoursInADay = 24;
const minutesInAnHour = 60;
const secondsInAMinute = 60;
const timeDifference = finalCutOffTime - Date.now();

const hoursToFinalCutOffTime = Math.floor(
  (timeDifference /
    (milliSecondsInASecond * minutesInAnHour * secondsInAMinute)) %
  hoursInADay
);

const minutesToFinalCutOffTime = Math.floor(
  (timeDifference / (milliSecondsInASecond * minutesInAnHour)) %
  secondsInAMinute
);

const secondsToFinalCutOffTime =
  Math.floor(timeDifference / milliSecondsInASecond) % secondsInAMinute;


return { secondsToFinalCutOffTime, minutesToFinalCutOffTime, hoursToFinalCutOffTime };

}

Answer №1

If you need a reference for your code, check out this updated example here. It's similar to the one you shared, with some variable name changes and consolidation into a single .ts file. The 'days' variable was removed, and the code assumes that 'orderCutOffTime' and 'timeString' are retrieved from subscriptions. If this meets your requirements, feel free to let me know.

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

Obtain the distinct highest value for every vertical axis on a bar graph

I'm facing an issue with my code where I am appending multiple SVG elements and generating 3 different charts. The problem is that the maximum value evaluated from the y.domain() for each y-axis is taken as the maximum value from all of my data. Is t ...

The language is being detected, but the translation feature is not functioning properly with i18n

I've configured the i18n middleware in my Express Node js server as follows: // server.js import i18nMiddleware from 'i18next-express-middleware'; import i18n from 'i18next'; import Backend from 'i18next-node-fs-backend'; ...

Canvas with a button placed on top

I am working with a canvas and trying to position an HTML element over it using CSS. My goal is for the button to remain in place on the canvas even when resizing the entire page. Here is the code I am currently using. https://jsfiddle.net/z4fhrhLc/ #but ...

tips for dynamically highlighting search bar after choosing a different dropdown option - vuejs

I need to filter products in my application based on name and category. To achieve this, I have included a search text box and a dropdown select box. The dropdown select box offers two options: 1. Search products by name 2. Search products by category name ...

The issue arises when attempting to use a JavaScript marker within an array, as it

At the moment, I am in the process of building a website that includes a Google map showcasing my custom markers. Each marker is linked to a specific URL, and the connection is straightforward (as shown with one of my markers below) - var image = 'p ...

Navigate to an Angular page URL using a Spring Boot REST API

I've been trying to figure out how to redirect to an Angular page from a Spring Boot API, but so far none of the methods I've tried have worked. Front end: (Angular) http://localhost:4200 Back end: (Spring Boot) http://localhost:80 ...

Animating the dimensions of objects in THREEjs

In my project using THREE.js, I am aiming to create a captivating animation where an object gradually shrinks into nothingness. After exploring solutions on Three.js - Animate object size, I found methods to adjust the size of an object. However, the chan ...

Displaying data on View is not working after navigation

I'm facing an issue where the data is not displaying on the view after routing, even though it appears in the console. Surprisingly, if I don't change the view, the data shows up. Additionally, if I call the service directly from the view, I can ...

Vercel Build Issue: It appears that the settings you are utilizing are intended for the 'client' module of '@sanity/preview-kit'

Hey there, I'm encountering a strange issue with Vercel deployment related to sanity. The specific error message during the Vercel build is: Error: It appears that you are using settings intended for '@sanity/preview-kit/client', such as &a ...

Executing Javascript prior to Gatsby page load

Currently, I am in the process of converting an HTML template using Bootstrap 5 into a Gatsby template. While the CSS and pages are functioning as expected, I have encountered an issue with the inclusion of a main.js file within the HTML template that need ...

Ways to troubleshoot problems with lifecycle scripts

Let me walk you through the steps that led to this error: I created a new folder for my project I opened the terminal within the folder and ran the command npm init I added an index.js file to the folder I ran the command npm install Finally, I executed t ...

bespoke slickgrid dropdown editor

I am currently working on implementing a slickgrid feature where a cell needs to display a dropdown (selectlist) with values fetched from a restful service. These values will vary for each row. The requirement is that the user should be able to select one ...

Can we enhance this JavaScript setup while still embracing the KISS principle (Keep it simple, Stupid)?

I have completed the following tasks: Ensured all event handlers are placed inside jQuery DOM ready to ensure that all events will only run Defined a var selector at the top of events for caching purposes Please refer to the comments below for what I be ...

What is the process for performing the "extract function" refactoring in JavaScript?

Are there any tools for extracting functions in JavaScript similar to the "extract function" refactoring feature available for Java and jQuery developers in Eclipse or Aptana? Or perhaps in another JavaScript/jQuery IDE? ...

Wait for the definition of a variable before returning in React Native

I am currently receiving data asynchronously and displaying it within the render() function using {data}. My dilemma is how to ensure that the render() function waits until the variable is defined. Currently, the placeholder variable remains the same or d ...

Is there a way to handle the ajax request only when necessary, instead of processing it every few seconds?

Currently, I am working on an AJAX chat system using PHP, MySQL, JavaScript, and AJAX. I have a piece of code that retrieves all chat messages within a div using AJAX, with the function running every 2 seconds. My issue lies in the fact that the div autom ...

Replace interface with a string

Is it possible to override an interface with a string in TypeScript? Consider this example: type RankList = "Manager" | "Staff" interface EmployeeList { id: string, name: string, department: string, rank: "Staff&q ...

Stop materializecss dropdown from closing when clicking within it

As I work on my current project, I am utilizing Materialize.css which includes a dropdown with various input forms inside. The dropdown can be closed by: clicking outside of .dropdown-content clicking inside of .dropdown-content clicking on .dropdown-bu ...

Is there a way to retrieve the redirected URL from an AJAX request?

Hi everyone, I'm currently trying to solve a puzzle regarding how to identify whether a PHP script redirects during an AJAX call. Does anyone have insight into whether JQuery AJAX has the capability to detect and keep track of location changes? Let&a ...

Encountering issues with proper function of history.listen within React Router

I am struggling to get my function to work every time React detects a change in the URL. The history.listen method is not triggering (the console.log statement is not showing up). I have read that this issue may be related to using BrowserRouter, but when ...