What steps can I take to resolve the glitching issue with my progress bar?

There seems to be a problem with the progress bar lagging behind. To see the issue in action, click on THIS LINK and go to the second song. The progress bar appears to be malfunctioning. If you have any solutions or suggestions, please assist! For a visual reference of the problem, check out this image. Below is the necessary code that I believe can help resolve this issue.

var timer;
var percent = 0;
var audio = document.getElementById("audioPlayer");
audio.addEventListener("playing", function(_event) {
  var duration = _event.target.duration;
  advance(duration, audio);
});
audio.addEventListener("pause", function(_event) {
  clearTimeout(timer);
});
var advance = function(duration, element) {
  var progress = document.getElementById("progress");
  increment = 10 / duration
  percent = Math.min(increment * element.currentTime * 10, 100);
  progress.style.width = percent + '%'
  startTimer(duration, element);
}
var startTimer = function(duration, element) {
  if (percent < 100) {
    timer = setTimeout(function() {
      advance(duration, element)
    }, 100);
  }
}
#timeline {
  width: 50%;
  height: 4px;
  background: rgba(0, 0, 0, .3);
  margin-top: 27px;
  float: left;
  margin-left: 10px;
  border-radius: 15px;
  background-color: blue;
}


/*Grabable Playhead*/

#playhead {
  cursor: pointer;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  margin-top: -10.9px;
  background: black;
}

.progress {
  height: 5px;
  background: black;
  transition: width .1s linear;
}
<audio id="audioPlayer" preload="true" ontimeupdate="initProgressBar()">
        <source src="https://tunechestmusic.000webhostapp.com/sleepy.mp3">      
    </audio>
<div id="wrapper">
  <!--Audio Player Interface-->
  <div id="audioplayer">
    <button id="pButton" class="play"></button>
    <div id="timeline">
      <div class="progress" id="progress"></div>
      <div id="playhead"></div>
    </div>
  </div>
</div>

Answer №1

Seems like there are a few vital components missing for it to function properly

First, your play button currently does nothing when clicked. You need to add an event listener to handle the click and trigger the audio playback

var playButton = document.getElementById('pButton');

playButton.addEventListener('click', e => { audio.play(); });

The second issue is that your audio tag calls a non-existent function initProgressBar.

<audio id="audioPlayer" preload="true" ontimeupdate="initProgressBar()">

A placeholder function has been included for you to complete or remove from the HTML if not needed.

function initProgressBar() {
  // TODO: What should be done here?
}

Once you've added the click event handler, the audio will play and the progress bar will update correctly. However, the circle doesn't track the progress yet, but it seems like you're still working on that part.

var timer;
var percent = 0;
var audio = document.getElementById("audioPlayer");
var playButton = document.getElementById('pButton');

playButton.addEventListener('click', e => { audio.play(); });

function initProgressBar() {
  // TODO: What should be done here?
}

audio.addEventListener("playing", function(_event) {
  var duration = _event.target.duration;
  advance(duration, audio);
});
audio.addEventListener("pause", function(_event) {
  clearTimeout(timer);
});
var advance = function(duration, element) {
  var progress = document.getElementById("progress");
  increment = 10 / duration
  percent = Math.min(increment * element.currentTime * 10, 100);
  progress.style.width = percent + '%'
  startTimer(duration, element);
}
var startTimer = function(duration, element) {
  if (percent < 100) {
    timer = setTimeout(function() {
      advance(duration, element)
    }, 100);
  }
}
#timeline {
  width: 50%;
  height: 4px;
  background: rgba(0, 0, 0, .3);
  margin-top: 27px;
  float: left;
  margin-left: 10px;
  border-radius: 15px;
  background-color: blue;
}


/*Grabable Playhead*/

#playhead {
  cursor: pointer;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  margin-top: -10.9px;
  background: black;
}

.progress {
  height: 5px;
  background: black;
  transition: width .1s linear;
}
<audio id="audioPlayer" preload="true" ontimeupdate="initProgressBar()">
        <source src="https://tunechestmusic.000webhostapp.com/sleepy.mp3">      
    </audio>
<div id="wrapper">
  <!--Audio Player Interface-->
  <div id="audioplayer">
    <button id="pButton" class="play">Play</button>
    <div id="timeline">
      <div class="progress" id="progress"></div>
      <div id="playhead"></div>
    </div>
  </div>
</div>

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

Unique Font Awesome Stylesheets

I'm facing a challenge with using Font Awesome to design my form. The FA icon comes pre-filled with color, but when I try to apply a specific color, it gets applied to the entire icon. Is there a way to work around this issue? Alternatively, should I ...

Polymer Integrated Binding for Advanced Search Options

Here is my code snippet to bind a tree in the Office->Team->"user" hierarchy. The main requirement is to enable user search functionality. To bind the tree, I have processed the users array to fit the required hierarchy. For searching, I filter out ...

Tips for resizing a 100% div without displaying vertical scrollbars in the browser

I am facing an issue with a table that has three rows. I have a main #container div with a height of 100%, and inside it is a table with 3 rows. The first and last row contain fixed size content. However, in the second row, there is a #content div with a h ...

Utilize a Vue.js filter on the v-model within an input element

Seeking assistance! I successfully created a directive that wraps the Jasny Bootstrap Plugin, specifically focusing on the input mask feature! Additionally, I have developed a custom filter using moment to format date fields! The date format received fro ...

JavaScript function using recursion returning an undefined value

I have created a function that generates a random number and checks if it already exists in an array. If it does, the function generates a new number until a unique one is found and adds it to the array. However, I am encountering an issue where the functi ...

Does defining the width and height of images go against the principles of material design?

Currently, I am immersed in a project utilizing material design. In this context, I find myself contemplating the size of an image that has been hard-coded as (200x200). I am curious to know if this approach aligns with the principles of material design. ...

Guide on adding space between rows in a table using HTML and Bootstrap 5

In the current table containing my content, it appears like this: https://i.sstatic.net/Mm7qT.png. I wish to add space between each row to create distinct separation and achieve a look similar to this: https://i.sstatic.net/ARgR1.png I experimented with ...

Tips for generating multiple HTML hyperlinks using a for loop in a Chrome extension

function createDropDown(){ const tree = document.createDocumentFragment(); const link = document.createElement('a'); for(let index = 0; index < urlList.length; index++){ link.appendChild(document.createTextNode(urlList[index])); ...

Currently trapped within the confines of a Next.js 13 application directory, grappling with the implementation of a

I need to figure out how to export a variable from one component to layout.tsx in such a way that it is not exported as a function, which is currently causing the conditional check in the class name to always be true. Below is the code snippet: // File w ...

Having trouble getting an HTML form to function with Ajax and PHP?

Seeking assistance from anyone who can lend a hand. I am delving into the complexities of Ajax, and I'm encountering issues where it seems like the script is being completely ignored or perhaps I'm just making a rookie mistake. Prior to display ...

Altering the color of a div based on a specified value condition

I am in need of assistance with a div structure similar to this: <div id="namacoc" tabindex="1" class="filled-text" placeholder="Input your name" contenteditable ></div> <div id="position" tabindex="1" class="filled-text" placeholder="Input ...

"What is the best way to eliminate duplicate data from an ng-repeat loop in AngularJS

I have a query regarding appending data from the second table into $scope.notiData in AngularJS. Additionally, I need to figure out how to remove ng-repeat data when the user clicks on the remove symbol X. I have attempted some code but it is not functioni ...

Glowing CSS animation surrounding a PNG image

.zuphologo { padding-top: 33%; padding-bottom: 2%; } .fx { box-shadow: 0px 0px 100px 4px #fff; animation: glow 1.5s linear infinite alternate; } @keyframes glow{ to { box-shadow: 0px 0px 30px 20px #fff; } } <div class="container"> ...

Using TypeORM in Javascript to create routes efficiently

After examining the TypeORM websites examples, I noticed that some of them demonstrate routing usage using TypeScript. Given that TypeORM has the capability to use JavaScript instead of TypeScript, I am seeking guidance on how to implement Express routing ...

How are the plugins configured for `postcss-import` implemented?

Recently, I've transitioned to using PostCSS exclusively with Webpack. As I explore the functionalities of using postcss-import to inline external stylesheets, I'm noticing that its options offer the ability to configure plugins and transformers ...

Analyzing a text file against a string to identify discrepancies using Node.js

Recently, I came across a dilemma involving a text file containing information on all Wifi connections (such as ssid, mac no, strength, etc.) that were within my laptop's range just 2 minutes ago. Now, I've rerun the code and obtained the current ...

Sorting a list with anchor tags alphabetically using Javascript/JQuery

Here is a list of services: <ul id="demoOne" class="demo"> <li><a href='http://site/Service.aspx?shId=1154'>Copy service for files from folder 12</a></li> <li><a href='http://site/Service.aspx? ...

In CSS and HTML, links located behind a divider are unable to be clicked

I am currently facing an issue with my code where the anchors on the left and right (behind the middle anchor) are not clickable. I have attempted to use z-index to bring only the anchors in front while leaving the divider in the back so that the middle an ...

Could one achieve the task without the presence of a function?

When I execute the below code: let app = express() in the console, it outputs: { [EventEmitter: app] _events: { mount: [Function: onmount] }, _eventsCount: 1, _maxListeners: undefined, setMaxListeners: [Function: setMaxListeners], getMaxList ...

What could be causing "Unknown property" errors when using unicode property escapes?

The MDN website provides examples of matching patterns with unicode support, such as: const sentence = 'A ticket to 大阪 costs ¥2000 ...