Continue iterating through the range of dates until the first date comes before the second date

I have a pair of Unix timestamps:

let start: number = 1632988953;
const end: number  = 1638259353;

My goal is to iterate over these two dates, recalculating the new start date in each iteration.

To achieve this, I am utilizing a while loop as shown below:

const datesArray = [];
while (start <= end) {
    let newDate = dayjs.unix(start).add(5, 'day').unix();

    datesArray.push(newDate);
    
    start = newDate;
}

However, upon executing this while loop within the function, it results in infinite iterations which ultimately leads to high resource consumption and crashes the browser. Can anyone help me identify what might be causing this issue?

Answer №1

Everything seems to be in order, feel free to test it out for yourself

let start = 1632988953;
const end  = 1638259353;

let condition = true;
console.log("before : ",condition);
const datesArray = [];
while (start <= end) {
    let newDate = dayjs.unix(start).add(5, 'day').unix();
    datesArray.push(newDate);
    start = newDate;
    condition = start <= end;
}
console.log("after : ",condition);
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8aca9b1a2bb88f9e6f0e6faf9">[email protected]</a>/dayjs.min.js"></script>
<script>dayjs().format()</script>

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

Checkbox selection limitation feature not functioning correctly

Having trouble with my checkbox question function - I want to limit the number of checkboxes that can be checked to 3, but it's still allowing more than that. I suspect the issue lies with latestcheck.checked = false; This is my typescript function: ...

Having trouble with a jQuery function that keeps throwing a TypeError about $(...) being null when trying to change an

I'm facing a frustrating issue right now - my hair is falling out as I try to understand why JQuery is constantly throwing a TypeError. I believe my call to replace an input value in the HTML is valid, but for some reason, it's not working. With ...

Adding various react elements to a list with the click of a button: A step-by-step guide

I am faced with a challenge where I have two different types of components that I want to display on the screen when a button is clicked. However, I don't want to simply show and hide the components. Instead, I want to be able to dynamically generate ...

Ways to verify the presence of users in the Database

I have successfully retrieved the data with the code below, but I am encountering issues with my script's if and else statements. Any tips or advice on how to improve this functionality? server.post('/like', (req, res,next) => { var ...

What is the best way to store JSON data in the state of a ReactJS application?

I need assistance with a data format related issue. The current format of the data is being set by someone else in the backend of the application. When the data is empty, it looks like this: "opening_time":{"Mon":[["0"],["0"]], "Tue":[["0"],["0"]], "Wed" ...

Transferring files to Django using AJAX

I am struggling with the process of uploading files to django using ajax. The upload is done within a modal window. The Form <div class="modal fade bs-example-modal-lg" id="fileUploadModal" role="dialog" aria-hidden="true"> <div class="modal ...

Utilizing PHP to dynamically load HTML forms and leveraging JQuery for form submissions

I am currently facing a dilemma that I am unsure how to approach. It seems that JQuery requires unique ID's in order to be called in the document ready function. I use PHP to read my MySQL table and print out HTML forms, each with a button that adds a ...

Calculating the sum of values in a specific position within an array of Javascript

Here is an array that needs to be updated: let arr = [ { "Id": 0, "Name": "Product 1", "Price": 10 }, { "Id": 0, "Name": "Product 1", "Price": 15 } ] I am looking for a way to add 1 to all the Price values, resulting in: let Final_arr = [ { ...

How can I utilize passed in parameters in Meteor React?

I am trying to figure out how to use two params that I have passed in the following example. Can someone please assist me? updater(layer, item){ this.setState({layer5: <img id="layer5" className="on-top img-responsive center-block" name="layer5" ...

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 ...

What is the process for invoking a page using a JavaScript function?

My index.php has a javascript code that calls a page in the following way: id = 5; var url = 'pages/pg/'+id+'/'; f.action = urlss.toLowerCase(); return true; The issue arises when I try to call the same page with a different ID, it do ...

Error: The property 'open' is not defined in the mdMenu object

Encountered a bug while working with angular-material design... When using md-menu, if a sub menu item is opened (as shown in the image) and then hovering over a non-subMenu item (menu item), it throws an error "Cannot read property 'open' of nul ...

Is there a way to identify whether the image file is present in my specific situation?

I have a collection of images laid out as shown below image1.png image2.png image3.png image4.png … … image20.png secondImg1.png secondImg2.png secondImg3.png secondImg4.png secondImg5.png ……. …….. secondImg18.png My goal is to dynamically ...

Having trouble extracting parameters with TypeScript in React Router even when they are present

I am in the process of migrating an older project to utilize react and react-router. Additionally, I am fairly new to typescript, which is the language used for this particular project. Any guidance or explanations on these topics would be highly beneficia ...

Having difficulty rearranging choices within an optgroup

This is a dropdown https://i.sstatic.net/Rk5yL.png <select id="officer-id" placeholder="Choose an officer"> <option selected="selected" >----</option> <optgroup id="pt1" label="To be reviewed"> ...

Exploring the usage of slots in WebComponents without relying on shadow DOM

I am working on developing a WebComponent without utilizing ShadowDOM. So far, everything has been going smoothly. However, I now aim to create a component that wraps other components similar to how it is done in Angular with @ViewChild / @ViewChildren. (I ...

Chrome successfully handles cross-domain AJAX calls with Windows authentication, whereas Firefox encounters issues with the same functionality

I am facing an issue with my WCF service that uses windows authentication. When I call this service using ajax in Google Chrome, everything works perfectly as the credentials are cached. However, in Firefox, I am receiving a 401 unauthorized error. I would ...

Modify/Adjust/Move the image uploaded by the user in VueJS before transferring it to an aws s3 bucket

Can you resize images in vuejs using vanilla js? I've managed to upload an image and send it to my s3 bucket using vue, but I'm struggling to figure out how to transform an image within vuejs and display it. Document.querySelector doesn't se ...

Tips for exploring electron without launching additional windows

Is there a way to navigate between HTML pages within the same window without opening multiple windows in Electron? ...

Rearrange list items by dragging and dropping

Here is the HTML and TypeScript code I have implemented for dragging and dropping list items from one div to another: HTML: <div class="listArea"> <h4> Drag and Drop List in Green Area: </h4> <ul class="unstyle"> <l ...