Issue with current time malfunctioning

For my latest project, I've been tasked with creating a karaoke application using javascript. The challenge is to synchronize the lyrics with the song as it plays.

Here's a snippet of my HTML code:

<div id="lyric"></div>
<audio id="audioPlayer" controls>
    <source src="http://supjs.fr/DEV2018/myKaraoke/music/LOR.mp3" type="audio/mpeg">
</audio>

The lyrics are stored in a text file and look something like this:

[00:00.00]Fanuilos heryn aglar

[00:10.00]Rîn athar annún-aearath

... (more lyrics)

Next, let's take a look at the javascript part:

window.onload = function(){
var track = document.getElementById('audioPlayer');

track.ontimeupdate = function(){
    console.log(this.currentTime);
    if (this.currentTime > 0 && this.curentTime < 9){
        getRSS(0);
    }
};
};

function getRSS(i) {

var rss = new XMLHttpRequest();
rss.open('GET', 'lyrics.txt', false);
rss.send(null);

var ligne = rss.responseText.split(/\n/g);
var linkRss = ligne[i];
document.write(linkRss);

}

However, I'm facing an issue where the lyrics display but the music stops and the audio disappears. I'm puzzled and unsure what could be causing this glitch. Can anyone provide some insight or help troubleshoot?

Answer №1

The issue lies in the fact that you are constantly replacing the content on your page with the document.write(linkRss); statement.

Instead, you should be adding text to your specific div element, rather than the entire document.

document.getElementById('lyric').innerHTML += linkRss;

It's important to note that combining time-based code with asynchronous code can lead to complications and make it difficult to achieve the desired functionality.

Answer №2

Here's a suggestion: const currentDate = new Date().getTime()

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

Sometimes, JQuery struggles to set input values accurately

Exploring the single page app sample provided here, I have encountered some anomalies when attempting to manipulate input controls with JQuery under certain conditions. Below is the consistent HTML structure followed by the JavaScript snippets in question. ...

The getElementById method in JavaScript can result in a null return value

Why is null returned by the getElementById method in JavaScript? <html> <head> <title>test_elementObject</title> <script language="JavaScript" type="text/javascript"> <!-- var input1 = document.getElementById ( " ...

Creating multipart/form-data with varying Content-Types for each part using JavaScript (or Angular)

Apologies for the confusion, please refer to my update below I have a requirement to link my AngularJS Project with an existing RESTful API. This API uses POST requests that involve uploading a file and submitting form data in a request. However, one of t ...

The callback function of the $.ajax statusCode method does not receive any parameters

As per the official jQuery documentation: If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error, they take the same parameters as the error callback. However, when I use th ...

How can the background of a div be altered when text is typed into an input field?

I need help with the following code. I want to be able to change the background color of a div with the class "target_bg" from red (default) to green every time someone enters or types text into the input field. <div class="target_bg"></div> & ...

Customizing date formats on HighCharts

Below is the code snippet I am currently working with: <script> $.getJSON('https://www.quandl.com/api/v3/datasets/OPEC/ORB.json?order=asc', function(json) { var hiJson = json.dataset.data.map(function(d) { return [new Date(d[0] ...

Get a Blob as a PNG File

Hope you had a wonderful Christmas holiday! Just to clarify, I am new to JS and may make some unconventional attempts in trying to download my Blob in PNG format. I am facing an issue with exporting all the visual content of a DIV in either PDF or image ...

Trouble with modifying style in Google Chrome and Internet Explorer prior to AJAX request

When utilizing AJAX, I have a common practice of setting up a loading indicator before each request to inform the user about the short wait. Usually, this is achieved by adding an animated loading gif. Interestingly, when performing this action in Firefox, ...

The search for the view in the directory "/views" was unsuccessful in Express 4.0

I've been working on a project using Express 4.0 and the Express3-handlebars libraries for NodeJS. Below is the setup: app.set('views', path.join(__dirname, 'views/')); app.engine('hbs', hbs({defaultLayout: 'main&a ...

Vue parent component not receiving events properly

Referring to these sources: Forum Post Stack Overflow Question In my project, I am utilizing: CodeSandbox Example The setup involves the parent component listening for events emitted by a child component: mounted() { this.$on("edit-category& ...

Adding a JavaScript object into the $http service

Struggling to correctly insert an object in this format: $scope.newObj = {1: "N/A", 2: "KO", 3: "OK", 4: "OK", 5: "OK", 15: "N/A", 19: "OK"} An attempt was made using the following loop: var objt = $scope.newObject; console.log($scope.newObject[0] ...

Deactivating elements on a website

Is there a way to prevent multiple transactions due to unintended repeated clicks on a button by disabling all webpage elements when the button is clicked? Suggestions include using a div that can be layered on top of the elements when the button is click ...

Easily accessible jQuery tabs with the option to directly link to a specific tab

I currently have tabs set up on my webpage. The code for these tabs is as follows: $('ul#tabs li a').click(function(){ var id = $(this).attr('id'); var counter = 1; $("ul#tabs a.current").removeClass('cur ...

Is it possible to generate an array of objects, where each object includes an observable retrieved through forkJoin?

let food = { id: 1, isTasty: false } Imagine I have a custom function that takes the ID of a food item and returns an observable which resolves to a boolean indicating whether the food is tasty or not. I wish to loop through an array of food items an ...

Retrieving user input through JavaScript and transmitting information using Ajax

UPDATE: Issue resolved by changing name="demo_name" and similar attributes on my form to id="demo_name". Thanks @Jill I'm attempting to save contact form data into a MySQL database using jquery's ajax feature. I'm new to this, and while it& ...

Update the HTML weather icon with data from JSON

I have a collection of weather icons stored on my computer. How can I customize the default weather icons with my own set of icons? In the JSON file, there is a variable like this: "icon":"partlycloudy" <html> <head> <script src="http://c ...

What methods can be used to ensure the document in mongoose is not deleted by updating the expires property on the createdAt field?

I have implemented account verification in this app and created a user account that is set to delete itself after 30 seconds if not verified. createdAt: { type: Date, default: Date.now, index: { expires: 30, }, }, However, I am now searching f ...

ASP.NET MVC and Modal Popup Extender: The Perfect Combination

Currently, I am working on an ASP.NET MVC application and aiming to incorporate Modal style windows. For instance, in my "Login" view, I include my login details along with a button for creating an account for the application. Even though I have a separat ...

Display upon hovering, conceal with a button located within a popup container

There seems to be an issue with the code below. Even though it works perfectly in jsfiddle, it breaks in my Chrome and other browsers right after displaying the ".popup" div. Can anyone point out what I might be doing wrong? I found similar code on this si ...

I'm seeking guidance on how to delete a specific ul li element by its id after making an ajax request to delete a corresponding row in MySQL database. I'm unsure about the correct placement for the jQuery

I have created an app that displays a list of income items. Each item is contained within an li element with a unique id, along with the item description and a small trash icon. Currently, I have implemented code that triggers an AJAX call when the user cl ...