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

Guide to transmitting and managing a JSON document utilizing JavaScript

When working on the server side, I receive a simple JSON file via REST that contains various IDs. Here is an example: [ { "_id": "5825a49dasdasdasd8417c1b6d5", } "_id": "dfsdfsdf4960932218417c1b6d5", } "_id": "23434344960932218417c1b6d5", },] To handle t ...

Delayed execution of Ajax request

Currently watching a Youtube video that explains the Call Stack and Event loop when dealing with Async events like Ajax calls. According to the video, JavaScript execution occurs line by line. Let's try running a sample program: var xhttp = new XMLHt ...

My API is feeding data to the Material UI CardMedia image

Has anyone encountered a similar error while using the CardMedia API provided by Material-UI? I am currently utilizing the Card & CardMedia components from material-ui to display data fetched from an api. However, I am facing difficulty in displaying ...

What is the best way to modify Mega Menus using JavaScript?

I am currently managing a website that features "mega menu" style menus. These menus consist of nested <UL> elements and contain approximately 150 to 200 entries, resulting in a heavy load on the page and posing challenges for screen readers. To add ...

I'm having trouble with my Selenium as it doesn't seem to be able to open

Hey there, I've been working on a script to login to Gmail, but I'm having trouble with entering the password after entering the email. public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "E:&b ...

Centered on the screen are the input field and corresponding label

I am in the process of creating a signup form, and I have encountered an issue. How can I make the input wider without using a fixed width like width: 420px? Additionally, I would like to center both the input field and the label. I envision something simi ...

Creating a curved exponential data set with specific endpoints and a set number of data points

Struggling to create a function that involves math skills, I could really use some assistance. The task is to design a function that takes data points x and generates an array of size x with exponentially increasing values from 0 to 100. It would be ideal ...

Retrieve the current URL upon page load

I'm currently attempting to parse the URL in document.ready() so I can retrieve the id of the current page and dynamically populate it with content from an AJAX call. However, I've encountered an issue where 'document.URL' seems to refe ...

Top method for preventing an HTTP 403 error when receiving the Set-Cookie header in order to establish the CSRF Cookie

As I interact with a REST API that includes CSRF protection measures, I am facing a common hurdle. Successfully obtaining the token and sending it back to the server seems to work smoothly. However, encountering an HTTP 403 error arises when initiating t ...

Utilizing cloud functions to distort an inappropriate image

I have a requirement to analyze any uploaded image for inappropriate content and blur it if necessary. The log this image is inappropriate indicates that the detection process is working correctly. However, I am not able to see any further logs which sugg ...

Require assistance in accessing the second tab on a webpage using JavaScript tabs

Currently, I have a web page with two tabs that are initially hidden until the corresponding tab button is clicked. The click event is managed by jQuery. For example, let's assume that tab 1 is the default one when the page loads. Now, I am looking fo ...

Unable to fetch data in CakePHP 3.x controller using jQuery AJAX POST

I've been searching everywhere and unfortunately, I can't seem to figure out why the data is not being sent to my controller when posting via ajax. Here's the jQuery code snippet: var newDate = {}; newDate['start' ...

What methods can I utilize to persuade Internet Explorer to directly show the content of application/json instead of prompting to download it?

While troubleshooting jQuery applications that utilize AJAX, I often find myself needing to inspect the JSON data returned by the service in the browser. To do this, I simply input the URL for the JSON data into the address bar. One advantage of using ASP ...

React application experiencing issues with MQTT and Mosquitto broker connectivity

Upon installing the Mosquitto broker, I successfully tested it in my command prompt. However, when I attempted to establish a connection with my React application, I encountered the error message: "WebSocket connection to 'ws://localhost:1883/' f ...

The tooltip feature in jQuery is experiencing some stuttering issues

Sometimes, images can convey messages better than words. I encountered a strange issue with my self-made jQuery tooltip. I prefer not to use any libraries because my needs are simple and I don't want unnecessary bloat. When I move my mouse from righ ...

Creating distinctive, unchanging colors for individual ellipses within a collection in p5.js

It's clear that each ellipse is part of a wave object, with the fill color applied on every frame causing a blinking effect. I'm trying to assign a random color to each ellipse when it's drawn, so it maintains that fill color instead of chan ...

Implementing Java Action through AJAX requests

As a newcomer to AJAX, I am faced with the task of populating a second drop-down list in JSP based on the selection made in the first drop-down. To achieve this functionality, I attempted to use AJAX within struct1.2. Within the onChangePrdGrp[JavaScript ...

Initiate an ajax call only when new data needs to be fetched for the initial load

I have a question regarding my use of Ext JS. I have been working with this framework for quite some time and am facing an issue that I can't seem to resolve. My application has a tree structure with nodes, and when a node is clicked, a corresponding ...

Having trouble viewing the image slider on an HTML webpage

I've been attempting to incorporate an image slider into my personal website on GitHub, but I've encountered some difficulties as the images are not displaying. Interestingly, I can load individual images without using the slider and they show up ...

The invocation of `prisma.profile.findUnique()` is invalid due to inconsistent column data. An invalid character 'u' was found at index 0, resulting in a malformed ObjectID

The project I'm working on is built using Next.js with Prisma and MongoDB integration. Below is the content of my Prisma schema file: generator client { provider = "prisma-client-js" } datasource db { provider = "mongodb" url = env("DATABA ...