Ich möchte registrieren, wie oft ein Song abgespielt wurde. Ich habe das funktioniert, aber ich möchte nicht, dass diese Anzahl von Zeiten zurückgesetzt wird, wenn der Browser geschlossen wird. Ich habe versucht mit Cookies zu arbeiten, konnte das aber nicht herausfinden.Nummer von Cookie lesen
Wir freuen uns auf Ihre Antworten!
HTML
<!DOCTYPE html>
<html>
<head>
<title>Showcase Titou Brouwers</title>
<meta charset ="UTF-8">
</head>
<body>
<audio id="music" autoplay="autoplay" controls="controls" onended="yourFunction()">
<source src="Data/1.mp3" type="audio/mp3" />
</audio>
<script src="App.js"></script>
<p>Total amount of sessions: </p>
<p id="demo"></p>
</body>
</html>
Javscript
//if music ends perform addSession
document.querySelector("#music").addEventListener("ended", addSession, false);
//Create cookie
document.cookie = "totalSessionsJ=0";
var totalSessions = document.cookie;
console.log(totalSessions);
//Increase totalSessions variable with 1
function addSession() {
document.cookie = totalSessions + 1;
//Display amount of performed sessions
document.getElementById("demo").innerHTML = totalSessions;
}
//Display 0 when no sessions have been performed yet
//document.getElementById("demo").innerHTML = 0;
/*
//If music ends perform yourFunction
document.querySelector("#music").addEventListener("ended", yourFunction, false);
//Let box pup-up in which you can type the score you give your session
function yourFunction() {
prompt('How many stars of five do you give this session?');
}
*/
EDIT
Nun habe ich folgendes, aber ich Schwierigkeiten mit 1 bis dem der Cookie-Wert hinzufügen. Außerdem möchte die letzte Codezeile nicht funktionieren.
//Create cookie
document.cookie = "totalSessionsJ=0; expires=Sun, 28 May 2028 12:00:00 UTC";
function getCookieVal() {
var cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)totalSessionsJ\s*\=\s*([^;]*).*$)|^.*$/, "$1");
return cookieValue;
}
//Increase totalSessions variable with 1
function addSession() {
var test = getCookieVal() + 1;
console.log(test);
document.cookie = "totalSessionsJ = test; expires=Sun, 28 May 2028 12:00:00 UTC";
//Display amount of performed sessions
document.getElementById("demo").innerHTML = getCookieVal();
}
//Display 0 when no sessions have been performed yet
document.getElementById("demo").innerHTML = "getCookieVal()";
ausgezeichnet, ich bin nur Schwierigkeiten haben, immer zu Ändern Sie den Wert des Cookies jetzt. – Bodhidharma482