Customized progress bar for monitoring lengthy JavaScript calculations

I am currently working on a project that involves using javascript with jquery and bootstrap. My main objective is to have a visually appealing progress bar displayed during heavy javascript computation. Despite knowing the exact progress state of the computation, I am facing an issue where CSS is not updating dynamically during the calculation process.

Although WebWorker is an option for some, it does not fit my specific requirements as I need to interact with a model in the main thread that cannot be easily copied or cloned to a worker.

For instance, currently the progress bar is only updated at the end of the computation. However, my goal is to have it update continuously throughout the process.

http://jsfiddle.net/K48B2/

Here is a snippet of the HTML code:

<div class="progress">
    <div id="loading" class="bar progress-bar progress-bar-info" style="width: 0%"></div>
</div>

And here is the corresponding javascript code:

for (var i = 0; i <= 10; i++) {
    $('#loading').css('width', i * 10 + '%');
    heavycomputation();
};

Answer №1

Jack shared this innovative solution that effectively... jsfiddle.net/nNZCE

function heavyProcessing(time, progress) {
    var startDate = new Date();
    var currentDate = null;
    do {
        currentDate = new Date();
    }
    while (currentDate - startDate < time);

    if (progress > 10)
        return;

    setTimeout(function() {
        console.log(progress);
        $('#loading').css('width', progress * 10 + '%');
        heavyProcessing(200, ++progress);
    }, 200);
}

heavyProcessing(200, 0);

Answer №2

Does this align more closely with your requirements?

function timeIntenseOperation(millis) {
    var date = new Date();
    var currentDate = null;
    do {
        currentDate = new Date();
    }
    while (currentDate - date < millis);
}

$(function() {
    for (var index = 0; index <= 100; index++) {
        setTimeout(function (index) {
           return function() {
                $('#loading').css('width', index + '%');
                $('#value').html(index + '%');
                console.log(index + '%');
                timeIntenseOperation(200);               
           }
        }(index), index * 200);  
    }    
});

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

Only apply prevent default on specific levels for better control

I am currently working on a menu with submenus. I am facing an issue where when I click on a top-level menu item, I need to use prevent default because they are anchor tags. However, for the submenu items, I do not want to prevent default behavior. I am st ...

"Vanishing Act: Title Toggle Disappears with a

Having trouble with a toggle feature that expands and collapses content. The issue I'm facing is that when the content opens, the button containing the title vanishes. Any insights on why this is happening and how to resolve it? <script> const s ...

Challenges with implementing speech recognition within a React component's state

I've encountered an issue with the React library react-speech-recognition. When trying to modify the newContent state within useEffect, it ends up printing as undefined. Additionally, I'm facing a similar problem when attempting to update the sta ...

Retrieve the element that was clicked by targeting its class name using JavaScript

Unfortunately, I couldn't create this example in JSFiddle as it's currently in read-only mode. My goal is to identify the specific element that was clicked based on a given class. var button = document.getElementsByClassName("mybutton"); button ...

Error in Displaying Vuetify Child Router View

I am currently working on integrating a child router-view to be displayed alongside surrounding components. Here is an overview of my routing setup: { path: "/login", name: "TheLoginView", component: TheLoginView, }, { path: "/dashboa ...

Should URL parameters be avoided as a method for retrieving data with React Router in a React application?

Within my application, there exists a main page labeled Home that contains a subpage named Posts. The routing structure is as follows: <Route path='/' element={<Home />} /> <Route path='/posts/popular' element={<Post ...

Fluid active tabs are causing alignment issues with CSS triangles

I have included CSS triangles on the active tabs with fluid width on this page, but they are not aligned properly in each tab. For example, the About Us tab is correctly and centrally aligned, but the others are too far to the right. The issue lies with t ...

Stylish tag colors in ExtJS 3 SuperBoxSelect

My SuperBoxSelect element is being populated from a remote store. I am looking to assign a different color to each tag when it is selected. The color value for each tag comes from the store. Currently, I am attempting to achieve this by using the followi ...

Optimizing React components by efficiently updating props without triggering unnecessary renders

As I delve into learning React, I've encountered a challenge with using a component to display details of a selected item in a table. The issue arises when clicking "Next" on the paginated table, causing the state to update and re-render the component ...

Verifying whether the file is an HTML image file type

My goal is to have a file input field where users can only select images. If an image is selected, the form should submit successfully, but if any other type of file is chosen, I want to display an error message. Here's what I have so far: $(document ...

Adding an element to the second-to-last position in a list

In the context of a HTML unordered list <ul id = "master_key_list"> <li id="master_key_23" class="available_element">Fix the Peanut Butter</li> <li id="master_key_24" class="available_element">Focus on Price Sensitivity</li& ...

Disable the checkbox functionality in Angular

Is there a way to disable clicking on a checkbox while still keeping an ng-click function attached? <input id="record-2" type="checkbox" class="checkbox" ng-click="doIfChecked()"> I've tried using ng-readonly and ng-disabled, as well as css p ...

Consolidate all data connected to the specified key from a JSON dataset

Looking at the json string presented below [{"_id":"9/17/2015","amt1":0,"amt2":13276.5},{"_id":"9/18/2015","amt1":8075,"amt2":6445.5}] The expected outcome is: [{"_id": ["9/17/2015", "9/18/2015"], "amt1": [0, 8075], "amt2": [13276.5, 6445.5]}] Is there ...

Is there a way to alphabetically sort V-Chips in Vuetify?

Hello, I'm currently attempting to organize multiple V-Chips alphabetically. Does anyone have any suggestions? Here is the code snippet I am working with. I want to display all my V-Chips in alphabetical order. Should I sort my array or is there a sp ...

Upon refreshing the datatable, I encountered a issue where the checkbox cannot be selected

After updating my data table with new content through an AJAX request, I am facing an issue where I am unable to select the check-boxes in the table. I use a class selector to choose the rows that contain multiple check-boxes. The select event is placed in ...

Having trouble figuring out how to make my Bootstrap Navbar more responsive

I'm having trouble with the responsive design of my Bootstrap navbar. I want all the buttons to be displayed in a row when on desktop, and then collapsed under the fas fa-bars navbar toggler as the viewport size gets smaller. All necessary stylesheet ...

Toggle the frequently asked questions feature by including a selector

I am trying to create a toggle FAQ feature. When the user clicks on the "+" symbol, it should expand and collapse the answer. I want to implement toggling functionality when clicking both the "+" symbol and the question title. Question: How can I toggl ...

Exploring the method of dynamically adding a row to a table on button click using Angular 2

In the midst of my Angular2 project, I am exploring the possibility of dynamically adding rows to a table when a button is clicked. While I am aware that *ngFor can achieve this, I am curious if it can be implemented only upon the clicking of a button. ...

Can we stop a specific container from resizing on mobile devices?

My goal is to have the entire page scale down, except for the navigation container. I'm adjusting some CSS rules to improve accessibility on smartphones, but the issue arises with the multiple images it uses. I need these images to display in their or ...

Unable to connect Dropzone to dynamically loaded DIV using AJAX

Using Dropzone for client-side image uploads has been a breeze. Take a look at this basic example that is currently up and running: If you examine the source code, you'll notice that I am utilizing JQuery to connect Dropzone to the upload1 div ID. H ...