Steps for adding a time display box to a time progress bar

There is a time progress bar with code provided. How can I make sure the yellow bar moves according to the time and display the time in a box?

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

var timer = 0,
    perc = 0,
    timeTotal = 2500,
    timeCount = 1,
    cFlag;

function updateProgress(percentage) {
    var x = (percentage/timeTotal)*100,
        y = x.toFixed(3);
    $('#pbar_innerdiv').css("width", x + "%");
    $('#pbar_innertext').text(y + "%");
}

function animateUpdate() {
    if(perc < timeTotal) {
        perc++;
        updateProgress(perc);
        timer = setTimeout(animateUpdate, timeCount);
    }
}

$(document).ready(function() {
    $('#pbar_outerdiv').click(function() {
        if (cFlag == undefined) {
            clearTimeout(timer);
            perc = 0;
            cFlag = true;
            animateUpdate();
        }
        else if (!cFlag) {
            cFlag = true;
            animateUpdate();
        }
        else {
            clearTimeout(timer);
            cFlag = false;
        }
    });
}); 
#pbar_outerdiv { cursor: pointer; }

Answer №1

In the updateProgress() method, you can easily update the percentage by changing one line of code:

$('#pbar_innertext').text((percentage / 100).toFixed(2) + " s");

Check out this JSFiddle for a live demo: https://jsfiddle.net/McNetic/hnfRe/395/

Update: After testing in a different browser, I noticed another issue - the animation duration may exceed the expected time of 2500 ms due to the high frame rate. To address this, reduce the number of animation frames and calculate the percentage based on actual time measurement. Here's an updated version with these changes:

https://jsfiddle.net/McNetic/hnfRe/396/

Answer №2

Take a look at this JSFiddle link. Feel free to customize the CSS properties such as colors and sizes to suit your preferences.

In essence, I placed the text outside the #pbar_innerdiv within a span element.

<div id="pbar_outerdiv">
   <div id="pbar_innerdiv"></div>
   <span id="pbar_innertext">Click!</span>
</div>

Update:

I have made some adjustments to the script that should better meet your requirements: JSFiddle Link. Here is the revised script that was utilized:

    var timer = 0,
    perc = 0,
    percIncreaser,
    timeTotal = 2500, //Adjust this value according to your needs
    timeCount = 1,
    secondsCount=0,
    cFlag;

function updateProgress(percentage,time) {
    $('#pbar_innerdiv').css("width", percentage + "%");
    $('#pbar_innertext').text(time/1000 + "s");
}

function animateUpdate() {
    if(perc < timeTotal) {
        perc+=percIncreaser;
        secondsCount+=10;
        updateProgress(perc,secondsCount);
        if(perc>=100) clearTimeout(timer);
        else timer = setTimeout(animateUpdate, timeCount);
    }
}
$(document).ready(function() {
    $('#pbar_outerdiv').click(function() {
        percIncreaser = 100/timeTotal*10;
        if (cFlag == undefined) {
            clearTimeout(timer);
            perc = 0;
            cFlag = true;
            animateUpdate();
        }
        else if (!cFlag) {
            cFlag = true;
            animateUpdate();
        }
        else {
            clearTimeout(timer);
            cFlag = false;
        }
    });
 });

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

Struggling with connecting AJAX to Django

Here is an example of how my HTML looks: $.ajax({ type: "GET", url:"/ajax/validate_supplier/", data:{ 'supplier_name': supplier_name }, dataType: 'json' }); This is the URL path set in my Django project: p ...

Ways to retrieve ID and update background

My goal is to alter the background color of a div when a user clicks on a button. There are 5 divs in total, each with a unique ID. PHP <?php $valorPremio = 0; ?> @foreach($premios as $premio) <div class="pure-g" data-id="{{$valorPremio ...

Obtain an Instagram access token with the help of Node.js

I've been attempting to acquire an access token from Instagram without utilizing a browser request, using the instagram-node module. However, I haven't been successful in receiving the access token. Can anyone provide me with a solution that does ...

Exploring recommendations using AngularJS

I am currently working on replicating the search suggestion feature found at: where certain words are displayed as you type in the search box. Here is my HTML setup: <form ng-controller="SearchCtrl"> <input name="q" ng-model="query" ng-keyp ...

Steps for preventing the inclusion of all .test classes from the page in the code: Utilize $( ".test" ).insertBefore( ".test1" ) to add only specific classes

What is the best way to prevent including all instances of .test classes from the page in the code? One solution could be using the following code: $( ".test" ).insertBefore( ".test1" ) <div id="testdiv"> <div class="test1"></div> <d ...

Leveraging PHP Variables within HTML Content

Hello everyone, I am facing an issue with reprogramming and would greatly appreciate some help. <div style="width:800px;height:100px;border:2px solid black;"> <html> <body> <br> <?php mysql_connect("localhost", "user", "passw ...

What is the best way to target an element within a div based on a specific attribute using JQUERY?

Can anyone help me figure out how to select a label within a div with a specific attribute and then trigger a click event? Specifically, I want the click event to only be triggered when the data-original-title attribute has a certain value (such as white). ...

Sending extra information with Yii2 form using ajax

When it comes to submitting a Yii2 form with an ajax call, I have encountered a situation where I need to include additional data along with the form's data. This extra information is in the form of a JSON encoded string that I created using JSON.stri ...

Removing information from firebase using a distinct identifier for targeting

My goal is to implement a delete functionality on my website by adding delete buttons next to each data entry displayed in the "#table_body". These buttons are dynamically created using JavaScript. The challenge I'm facing is how to remove the data ba ...

Monitoring the sequence of function calls within mock functions in Node.js

I am facing a challenge in writing an automated test that tracks the functions call chain. The scenario involves two functions, nativefn1 and nativefn2, where I cannot modify their code to enable tracking. I need to come up with a workaround for this situa ...

Adding additional information to the end of a table using JQuery

<table id="myTable0" name="myTable0"> <tbody> <tr> </tr> </tbody> </table> <table id="myTable1" name="myTable1"> <tbody> <tr> </tr> </tbody> </table> I am seeking a solution in JQuer ...

What is the best way to use jQuery to wrap two elements while maintaining the integrity of their parent

I'm currently facing an issue with wrapping two elements in jQuery. Occasionally, there may be an invisible duplicate of the parent element present. When this happens, the element from the hidden div also gets wrapped in the visible one: jQuery(d ...

Customize CSS to target the first and last elements in a row using flexbox styling

I am facing a challenge in selecting the last element of the first row and the first element of the last row within a flex container. The setup involves a flex-wrap: wrap; for my flex container where all elements have flex: auto; with different sizes. Thi ...

Using a combination of look-arounds and tag omission for advanced parsing

I am trying to identify text that is not part of another word (which I have working successfully), but I also want to ensure that the text is not inside an <a> tag. "Java <li>Javascript</li> <a href="">Some Java here</a> more ...

What is the best method for organizing an array of objects based on their subobjects?

When developing a web app's back-end with TypeScript, I've found intersection types to be incredibly beneficial for optimizing SQL queries. Imagine having the following tables: User userId: number userEmail: string Post postId: number userId: ...

The attempt to create a React app was unsuccessful after running the command `npm init react-app my-app

Can anyone assist me in resolving this error? https://i.sstatic.net/mXA3o.png I utilized npm to generate the React application. Unfortunately, utilizing npx create-react-app is not an option as it takes over 40 minutes to create the app even with a high- ...

The Regex.Replace function is not functioning as expected

I am currently faced with the challenge of replacing HTML tags within a string. For example, I need to convert <strong>text</strong> to <b>text</b>, among other similar replacements (although I understand that the b tag is considere ...

Place Div in the center of another Div

In my maze creation, I am trying to center an inner div, but no matter what I do, using margin: 0 auto; does not seem to work The inner div displays a sad smiley face when the user enters a wall and loses #highlight_lose { width: 550px; height:55 ...

Ways to verify if a particular value is included in a list

I am faced with a situation where I need to display different content in HTML based on the presence of a specific value ("sign_change_global") in a JSON loaded from a server. I believe this issue can be resolved within HTML only, but I also wonder if there ...

Conceal the iframe if the source is http:// and there is no associated

Is there a way to hide the iframe only if the src starts with "http://"? This is what I have tried so far: <script type="text/javascript> if ( $('iframe[src^="http://"]') ) document.getElementById('iframe').style.d ...