Dynamic progress bar based on time remaining

Is it possible to create a progress bar that reflects the countdown timer displayed in a div? The timer counts down from 7 minutes and is shown as follows:

<div id = "quiz-time-left"> 0:06:24 </ div>

I want to ensure that if the page is reloaded, the progress bar does not reset. Can this be achieved using CSS and a script?

The following is the CSS code for the progress bar:

 
#quiz-time-left {    
    width: 0;
    height: 100%;
    -webkit-animation: progreso 420s linear;
            animation: progreso 420s linear;
    color: #eb7260;
    background: #eb7260;
 }
#page-mod-quiz-summary #quiz-timer {
    text-align: center;
    display: none;
    position: absolute;
    left: -50px;
    top: -50px;
}
#quiz-timer {
    border: 2px solid #eb7260;
    text-indent: -9999px;
    display:block;
    font-size: 0px;
    height: 35px;
}
@-webkit-keyframes progreso {
    0% {
        width: 100%;;
    }
    100% {
        width: 0;
    }
}

@keyframes progreso {
    0% {
        width: 100%;
    }
    100% {
        width: 0;
    }
}

Here is the corresponding HTML code:


<div id="quiz-timer" role="timer" aria-atomic="true" aria-relevant="text" style="display: block;">Time left: <div id="quiz-time-left">0:06:41</div></div>

Your assistance would be greatly appreciated. Thank you and best regards.

Answer №1

One way to approach this is by utilizing localStorage to save the current status of the timer and reduce this value every second.

Upon completing page loading, you can check if the localStorage is defined. If not, set it to 420. On the other hand, if localStorage is defined, resume the countdown from the saved time in localStorage.

Answer №2

Here is a simple way to incorporate this feature:

Remember to store the remaining time in localStorage.

window.onbeforeunload = function(timeLeft) {
       localStorage.setItem('timeRemaining', timeLeft);
    }

Upon page load, confirm if there is an item labeled timeRemaining and continue the countdown using that value:

if(localStorage['timeRemaining']) {
    resumeCountdown(localStorage.getItem('timeRemaining'));
}

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

Discovering a CSS value using jQueryUncovering the CSS value with jQuery

Currently, I have a script that allows me to change the color of an event in a calendar to red when clicked. eventClick: function(calEvent, jsEvent, view) { $(this).css('border-color', 'red'); } Now, I want to enhance this featur ...

Is it possible for jQuery to execute in a sequential manner?

Below is the code I am working with: https://jsfiddle.net/c4zquo60/1/ CSS: #bg { background-repeat: no-repeat; position: absolute; top:0; bottom:0; left:0; right:0; width:100wh; height:100vh; z-index: -1; opacity: ...

Utilizing jQuery to apply a class to an element and modify the CSS parameters with custom values simultaneously

Unsure if this idea is feasible, but I'll attempt to explain my requirements. I am interested in accomplishing something like the following: A CSS class with variables X, Y, and Z left undefined: .r {border-radius:Xpx;-webkit-border-radius:Ypx;-moz ...

When using the "top" property in position-absolute, the child element may exceed the boundaries of the parent element

My child element is overflowing its parent when I apply "top: random pixel" to it. It seems to be using the top position relative to the body of the page. // HTML <div class="movie-list"> <div class = "inn ...

The computed function functions smoothly in one component but encounters issues in a different one

After adding multiple components to a page, I noticed that updates made to a parent object are reflected in one component but not the other. The main page, PatientEditor.vue, includes the following components: <notes-editor v-model="pt" /> <char ...

What is the best way to create a sortable column that is based on a nested object within data.record?

I am utilizing jquery jtable to showcase some data in a table. I have been using the following code snippet for each field in the table to display the data and enable sorting: sorting: true, display: (data) => { return data.record.<whatever_value ...

Arrangement of elements using Bootstrap's order and column classes beneath

I'm having trouble with a bootstrap layout. Here is the layout I need to achieve on different screen sizes: On mobile, I want: Title Image Text And on desktop, I want: Image Title Text However, the title div always takes up the image he ...

iPhone 3GS and 4 not expanding DIVs by 100%

I've been encountering a significant issue with a div that's set to 100%. It displays properly on desktop screens, but on iPhones, there appears to be a gap on the right side. I've attempted to use the following CSS: body { min-width:980p ...

using jquery to retrieve the current time and compare it

This is the code I currently have: var currentTime = new Date() var month = currentTime.getMonth() + 1 var day = currentTime.getDate() var year = currentTime.getFullYear() var hours = currentTime.getHours() var minutes = currentTime.getMinutes() aler ...

Cease the act of sending submissions through JavaScript ajax requests

I need to prevent a form submission based on the result of an ajax request... Here is the code I attempted to implement: The chreg() function is supposed to return a boolean value from the ajax request, but it's not working! Javascript: function ch ...

Exploring ways to display a JavaScript object on click from a .json file

As I delve into the world of javascript and json, I find myself facing a challenge. I am looking to extract information (using the Get Information function) from a json file within a javascript function triggered by an event. The catch is, I want to accom ...

Why isn't my state being updated properly with React's useEffect, useState, setInterval, and setTimeout functions?

const handleClick = () => { if (!activated) { if (inputValue == '') { return } if (!isNodeInGraph(graph, inputValue)) { return } } setActiv ...

Responsive design in Bootstrap(-Vue) with compact input fields

I am currently working on creating an input form with small fields and signs between them. https://i.sstatic.net/THjhs.png Right now, I am using a combination of classic HTML and Bootstrap-Vue, as shown in the screenshot. It is functional, but the issue ...

Resolving the "TypeError: Unable to access the 'then' property of undefined" issue in Node.js

I am currently working on connecting my app to a third party API using a node module. Below is the code I am utilizing for a school project. The library I am using to request data from the API can be found here. Note: This package is not being maintained ...

Unusual Type Inference in Typescript {} when Evaluating Null or Undefined

Upon upgrading from typescript 4.9.3 to 5.0.2, we encountered an error when asserting types. Can someone explain why the function "wontWorking" is not functioning correctly? I expected this function to infer v as Record<string, any>, but instead it ...

Utilizing CSS to expand a section of an image from a sprite sheet across a div

I need to resize a div that is 1x5 and display an embedded image from a sprite sheet. I am looking for a CSS solution to either repeat or expand this div to cover the area of a larger div. Is there a way to achieve this using only CSS, or will it require ...

What is the method for attaching a div to another div using javascript?

function displayContent(a) { var content = $("#" + a).html().trim(); alert(content); $('#temstoredivid').append( "<div class='maincover' id='maincov1'>" + " <div class='subcover' id='sub ...

Explore an array of objects to find and retrieve a value from the final matching object in JavaScript

I'm having difficulty retrieving the value for the last event that includes "SearchResults" in the scenario outlined below: Here is a schema of my datalayer where I am looking to gather information. Currently, I have managed to write the following co ...

"Retrieve JSON data from MongoDB without any extra spaces or formatting, also known as unpretty

In my current setup with mongodb 2.2.0, I am working on a way to display json data in a single line format instead of the "pretty" printing typically achieved with printjson() or find().pretty(). Essentially, I want the documents to be listed in json forma ...

Combining Jquery with Append to Dynamically Update PHP Variables

Code Snippet (index.html) <script> $(document).ready(function() { var interval = setInterval(function() { $.get("load_txt.php", { 'var1': 4, 'var2' : 52}, function(data){ $('#msg' ...