I'm looking for the most easiest way to fill a progress bar using JS, the progress bar itself should be filled dynamically.
I've received the following code so far from a friend:
var goal = moment.utc('2023-03-16 21:59:03')
var start = moment.utc('2023-03-15 21:59:03')
var now = moment.utc()
var percComplete = 0;
var countdownProgress = function () {
now = moment.utc()
var diff = goal.diff(now);
var time = moment.utc(diff).format('HH:mm:ss');
var percComplete = (now - start) / (goal - start) * 100;
console.log(goal - now);
if (percComplete < 100) {
document.querySelector('.js-countdown-text').innerHTML = `${time}`;
document.querySelector('.js-countdown-progress').setAttribute('style', `width: ${percComplete}%;`);
} else {
document.querySelector('.js-countdown-text').innerHTML = `00:00:00`;
document.querySelector('.js-countdown-progress').setAttribute('style', `width: 100%;`);
}
I want to get rid of the start
var, because the start
time should be "now" all the time. Is there an easy way to modify this code to keep the functionality, but just get rid of this "start in the past" feature?
When I just use:
var percComplete = now / (goal - now) * 100;
it won't work and I don't understand exactly how to the calculation process itself using JS.