Sequential calls to moment.utc() do not necessarily follow a consecutive order

When implementing Knockout in TypeScript, the following code is used:

    class timeHelpers {    

    private static createTimeBasedObservable(updatePeriodInMillis: number) {
        const nowTime: KnockoutObservable<moment.Moment> = ko.observable<moment.Moment>();
        let interval: number;

        const computed = ko.pureComputed(() => nowTime());

        computed.subscribe(() => {
            nowTime(moment.utc());
            interval = setInterval(() => nowTime(moment.utc()), updatePeriodInMillis);


        }, this, "awake");

        computed.subscribe(() => {
            clearInterval(interval);
            interval = undefined;
        }, this, "asleep");

        return computed;
    }

    static readonly utcNowWithMinutePrecision = timeHelpers.createTimeBasedObservable(60 * 1000);
    static readonly utcNowWithSecondPrecision = timeHelpers.createTimeBasedObservable(1000);

export = timeHelpers;
}

An issue arises with consecutive calls to production server values that are not always sequential.

   ...
   const now = timeHelpers.utcNowWithSecondPrecision();
   now.add(moment.duration(this.serverTimeDifference())); 
   // this.serverTimeDifference() is some numerical value
   ...

This inconsistency can be observed in the console printout link provided below:

https://i.sstatic.net/DeuXA.png

Answer №1

When you use the 'add' method, it changes the original moment by adding the specified time to it. https://example.com/moment/add-method-explanation

To resolve the issue, I made sure to apply the 'add' method to a cloned value:

  const newTime = now.clone().add(moment.duration(this.serverTimeDifference())); 

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

What tool can be used for formatting and syntax highlighting when working with ejs (embedded javascript) templates?

When working on my project, I utilize EJS as the express templating engine. While it is user-friendly and efficient, I have encountered difficulties when it comes to editing files - the text highlighting could be better, and I have not been able to find an ...

Improving render speed in Angular 7 FormArray

Currently, I am in the process of developing an angular component that is responsible for rendering and modifying invoices. In order to edit the line items within the invoice, I have implemented a FormGroup containing a FormArray of line item forms: lineI ...

Express Node fails to launch

I recently decided to try my hand at Express and learn more about it. However, I encountered an issue while trying to start a server. Initially, I attempted to access 127.0.0.1:8080 through Express but nothing seemed to be happening. Although I copied most ...

Retrieving Extensive Information for Display on Data Table - Incorporating ReactJS

I am currently facing a challenge with my web page that contains a table populated with 20,000 rows of data fetched from my database. To enhance performance, I have stored this large dataset in localStorage. However, when users navigate to this particular ...

Ensure that the HTTP GET request completes before initializing Angular using the ngOnInit lifecycle hook

Overview I am working on an Angular application that consists of three components: root, child1 (tabs.component.ts), and child2 (io.component.ts). Additionally, there is a service in place that communicates with a Tomcat server by sending get and post req ...

The data from my ajax code is not being successfully transmitted to the database

I'm having trouble using ajax to add data to the database. I'm not sure if the issue lies in the ajax code or the PHP code. Here is a snippet of my code: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></ ...

Discovering dynamically created controls within a repeater using JQuery

Struggling to locate textbox values within my repeater control. Upon button click, I need to retrieve the textbox value associated with the specific button that was clicked. Here is an example: Textbox1 ----> should be linked to Button1 TextBox2 ----& ...

Customize the Color of the Selected Button on the Menu (Implementing Ant Design Components in React)

Hello, I am reaching out to inquire about the process of updating the menu associated with a specific button in Ant Design using React. I am interested in changing the options that are displayed when the button is clicked. I would greatly appreciate any g ...

The performance of the Ajax Jquery remove function leaves something to be desired

My table has items with a delete button for each row. To achieve this, I created the following Ajax function: $(document).ready(function() { $(".destroy-device").click(function(e) { e.preventDefault(); var id = $(this).attr("data-id"); $.aj ...

Update the content that corresponds to the regular expression in jQuery

I am on a quest to locate specific content that matches a regular expression across an entire webpage and then replace it with different text. Below is the code I have been utilizing for this task. var reg = /exam+ple/; $("body").html(function () { ...

What is the best way to perform nested data filtering in mongoose?

I am working with a User entity that contains a role property: @Prop({ type: Types.ObjectId, ref: 'Roles' }) role: Role | Types.ObjectId; The Role interface looks like this: export interface Role { _id: Types.ObjectId; name: string; perm ...

The angular framework is unable to assign a value to the property 'xxxx' because it is currently undefined

I am currently working on a simple application using Ionic (angular) and I am facing an issue with the error message: Cannot set property 'origin' of undefined Below is the interface for Product.ts: export interface Products{ id: number ...

Unable to retrieve data from the JSON object

I'm struggling to extract the data value from a JSON object. Let me share my code snippet: var ab_id = $( "#ab_id" ).val(); $.ajax({ type: 'GET', contentType: 'application/json', url: 'edit_account.php', ...

Why is it that a click event outside of an HTML element cannot be detected in this Vue 3 application?

I've been diving into building a Single Page Application using Vue 3, TypeScript, and The Movie Database (TMDB). Currently, I'm focused on developing a search form within my project. Within the file src\components\TopBar.vue, here&apo ...

Issues with loading Angular 9 application on Internet Explorer 11

Having trouble with my app not loading in IE 11 after adding ngx-treeview. Encountering the error (SCRIPT1002: Syntax error), Script Error Error point in vendor.js Unsure how to resolve this issue. Works fine in chrome and firefox, but in IE11 all I se ...

JavaScript - Getting the name of a sub-class when extending the Error class

In my application, I have custom Errors that I wanted to check for later using the constructor name. However, when I extend Error in my classes, the constructor name always displays as "Error" instead of the actual name I assigned to it. I conducted some ...

Verify whether a group of div elements overlaps a fixed div while scrolling

I have a layout with a list of posts and a logo positioned at the bottom of the page. The issue I am facing is that sometimes the title of the posts and the logo overlap, and I want to set the logo's opacity to 0.25 only when the text from .cat-date i ...

execute bower install for the specified bower.json file

Let's say my current working directory is c:\foo\ while the script is running. I want to execute bower from there for the c:\foo\bar\bower.json file. This can be done in npm by using npm install --prefix c:\foo\bar. ...

Adjusting the color of a div based on the selection of a radio button contained within it

Looking for a way to change the color of a div when a radio button inside it is selected. I have been searching for a solution and haven't found anything that works yet. I want to display two divs side by side, each saying "choose this plan", with a c ...

Cookies in Node.js Express are not being incorporated

Currently, I am in the process of developing a nodejs application and facing an issue with defining cookies. Here is a snippet of the code that I am working with: var app = express(); app.set('port', process.env.PORT || 3000); app.set('vie ...