webmusic.js: code improovements

This commit is contained in:
Leah 2021-03-06 20:36:43 +01:00 committed by Leah (ctucx)
parent 81b67db9a1
commit 822996211f
1 changed files with 13 additions and 8 deletions

View File

@ -67,22 +67,26 @@ window.onkeyup = function (event) {
function togglePlayback() {
if (sound.playing())
if (sound.playing()) {
sound.pause();
else
} else {
sound.play();
}
}
function setState(state) {
gstate = state;
console.log("now in state: " + state);
updateState();
}
function playSong(url) {
if (document.getElementsByClassName("playing").length > 0)
if (document.getElementsByClassName("playing").length > 0) {
document.getElementsByClassName("playing")[0].classList.remove("playing");
}
index = queue.indexOf(url);
sound.stop();
sound.unload();
@ -134,8 +138,9 @@ function updateState() {
document.getElementById("state").setAttribute('onclick', 'togglePlayback()');
let statestr = "[";
statestr += gstate;
if (sound.playing())
if (sound.playing()) {
statestr += " " + formatTime(Math.round(sound.seek())) + "/" + formatTime(Math.round(sound.duration()));
}
statestr += "]";
document.getElementById("state").innerHTML = statestr;
@ -157,16 +162,16 @@ function initState() {
}
function previousTrack() {
if (index-- === 0)
index = total-1;
if (index-- === 0) index = total-1;
if (continuelist) {
playSong(queue[index])
}
}
function nextTrack() {
if (++index === total)
index = 0;
if (++index === total) index = 0;
if (continuelist) {
playSong(queue[index])
}