Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- // Variablen und Funktionen
- let i = 1;
- let w = 1;
- let secondsWord = document.getElementById('secondsWord');
- const againAfterTimeout = document.getElementById('againAfterTimeout');
- const againFromStart = document.getElementById('againFromStart');
- const answerReplay = document.getElementById('answerReplay');
- const answerReplayRight = document.getElementById('answerReplayRight');
- const blackOverlay = document.getElementById('blackOverlay');
- const buttonFour = document.getElementById('4');
- const buttonOne = document.getElementById('1');
- const buttonThree = document.getElementById('3');
- const buttonTwo = document.getElementById('2');
- const count = document.getElementById('count');
- const countPlayTime = document.getElementById('countPlayTime');
- const countPlayTimeOver = document.getElementById('countPlayTimeOver');
- const countQuestions = document.getElementById('countQuestions');
- const finalOverlay = document.getElementById('finalOverlay');
- const greenOverlay = document.getElementById('greenOverlay');
- const makeItBetterFour = document.getElementById('makeItBetterFour');
- const makeItBetterOne = document.getElementById('makeItBetterOne');
- const makeItBetterThree = document.getElementById('makeItBetterThree');
- const makeItBetterTwo = document.getElementById('makeItBetterTwo');
- const next = document.getElementById('next');
- const noMoreQuestions = document.getElementById('noMoreQuestions');
- const numberSeconds = document.getElementById('numberSeconds');
- const playpart = document.getElementById('playpart');
- const points = document.getElementById('points');
- const pointsEnd = document.getElementById('pointsEnd');
- const prologue = document.getElementById('prologue');
- const question = document.getElementById('question');
- const questionAnswers = document.getElementById('questionAnswers');
- const questionReplay = document.getElementById('questionReplay');
- const questionReplayRight = document.getElementById('questionReplayRight');
- const redOverlay = document.getElementById('redOverlay');
- const rightOne = document.getElementById('rightOne');
- const start = document.getElementById('start');
- const wrongFour = document.getElementById('wrongFour');
- const wrongOne = document.getElementById('wrongOne');
- const wrongThree = document.getElementById('wrongThree');
- const wrongTwo = document.getElementById('wrongTwo');
- // !!! Nodelist mit den Buttons für die Antworten bereit stellen:
- const answerBtns = document.querySelectorAll('div.answers button');
- blackOverlay.style.top = '-' + window.innerHeight + 'px';
- finalOverlay.style.top = '-' + window.innerHeight + 'px';
- function answerBlockScale() {
- let answerBlock, z;
- answerBlock = document.querySelectorAll(".answerBlock");
- for (z = 0; z < answerBlock.length; z++) {
- answerBlock[z].style.cssText = 'transform: scale(1, 1);'
- }
- }
- function answerBlockReScale() {
- let answerBlock, z;
- answerBlock = document.querySelectorAll(".answerBlock");
- for (z = 0; z < answerBlock.length; z++) {
- answerBlock[z].style.cssText = 'transform: scale(0, 0);'
- }
- }
- function wrongAnswerBlock() {
- playpart.style.cssText = 'opacity: .5; transition: opacity 2s';
- redOverlay.style.cssText = 'display: grid';
- stopTimer();
- setTimeout(function () {
- redOverlay.style.cssText = 'display: grid; transform: translateY(0)';
- },
- 700);
- setTimeout(function () {
- answerBlockScale()
- },
- 1100);
- }
- function rightAnswerBlock() {
- playpart.style.cssText = 'opacity: .5; transition: opacity 2s';
- greenOverlay.style.cssText = 'display: grid';
- setTimeout(function () {
- greenOverlay.style.cssText = 'display: grid; transform: translateY(0)';
- },
- 700);
- setTimeout(function () {
- answerBlockScale()
- },
- 1100);
- }
- function redOverlayTransform() {
- redOverlay.style.transform = 'translateY(-101%)';
- playpart.style.cssText = 'opacity: 1; transition: opacity 2s';
- }
- function greenOverlayTransform() {
- greenOverlay.style.transform = 'translateY(-101%)';
- playpart.style.cssText = 'opacity: 1; transition: opacity 2s';
- }
- let sec = 21;
- // numberSeconds.innerHTML = sec - 1;
- // let stoptime = true;
- // countPlayTime.innerHTML = 'Countdown startet ...'
- // countPlayTime.style.backgroundColor = 'gray';
- // countPlayTime.style.color = 'white';
- function startTimer() {
- // if (stoptime == true) {
- // stoptime = false;
- // timerCycle();
- // }
- }
- function stopTimer() {
- // if (stoptime == false) {
- // stoptime = true;
- // }
- // countPlayTime.innerHTML = 'Countdown angehalten ...'
- // countPlayTime.style.backgroundColor = 'gray';
- // countPlayTime.style.color = 'white';
- }
- function resetTimer() {
- // sec = 21;
- // stoptime = true;
- // countPlayTime.innerHTML = 'Countdown startet ...'
- // countPlayTime.style.backgroundColor = 'gray';
- // countPlayTime.style.color = 'white';
- }
- function timerCycle() {
- if (stoptime == false) {
- sec = parseInt(sec);
- sec = sec - 1;
- secondsWord = 'Sekunden';
- if (sec == 1) {
- secondsWord = 'Sekunde';
- }
- countPlayTime.innerHTML = 'Zeit läuft! Noch <span class="nowrap">' + sec + ' ' + secondsWord + ' ...</span>';
- if (sec <= 20) {
- countPlayTime.style.backgroundColor = 'rgb(118, 170, 14)';
- countPlayTime.style.color = 'white';
- }
- if (sec <= 10) {
- countPlayTime.style.backgroundColor = 'gold';
- countPlayTime.style.color = '#444';
- }
- if (sec <= 3 && sec != 0) {
- countPlayTime.style.backgroundColor = 'crimson';
- countPlayTime.style.color = 'white';
- }
- if (sec == 0) {
- stopTimer()
- countPlayTime.style.display = 'none';
- countPlayTimeOver.style.display = 'block';
- questionAnswers.style.cssText = 'display: grid; opacity: 0; transform: rotateY(180deg); transition: opacity 6s, transform 3s ease-in';
- blackOverlay.style.cssText = 'display: grid';
- blackOverlay.style.top = '-' + window.innerHeight + 'px';
- setTimeout(function () {
- blackOverlay.style.cssText = 'display: grid; top: 0; transition: top 2s ease-in';
- },
- 2000);
- }
- setTimeout("timerCycle()", 1000);
- }
- }
- function doItAgain() {
- i = 1;
- makeQuestion();
- resetTimer();
- blackOverlay.style.top = '-' + window.innerHeight + 'px';
- finalOverlay.style.top = '-' + window.innerHeight + 'px';
- countPlayTime.style.display = 'block';
- countPlayTimeOver.style.display = 'none';
- questionAnswers.style.cssText = 'display: grid; opacity: 1; transform: rotateY(0); transition: opacity 6s, transform 3s ease-in';
- redOverlay.style.transform = 'translateY(-101%)';
- greenOverlay.style.transform = 'translateY(-101%)';
- playpart.style.cssText = 'opacity: 1';
- setTimeout(function () {
- startTimer();
- },
- 5000);
- }
- function pointerEventsOff() {
- buttonOne.style.pointerEvents = 'none';
- buttonTwo.style.pointerEvents = 'none';
- buttonThree.style.pointerEvents = 'none';
- buttonFour.style.pointerEvents = 'none';
- }
- function pointerEventsOn() {
- buttonOne.style.pointerEvents = 'auto';
- buttonTwo.style.pointerEvents = 'auto';
- buttonThree.style.pointerEvents = 'auto';
- buttonFour.style.pointerEvents = 'auto';
- }
- // Ermittelt die Anzahl der Fragen und ruft die Callback-Funktion auf:
- function getCountRows(callback) {
- fetch('countRows.php', {
- method: 'post',
- }).then(countofquestions => {
- return countofquestions.json();
- }).then(countofquestions => {
- callback(countofquestions);
- });
- }
- // Erstellt eine Frage, deren Antwortmöglichkeiten und die Punktzahl:
- function makeQuestion(countofquestions) {
- const oneQuestion = new FormData();
- oneQuestion.append('id', i);
- fetch('getQuestions.php', {
- method: 'post',
- body: oneQuestion
- }).then(questions => {
- return questions.json();
- }).then(questions => {
- count.innerHTML = i + '. Frage:';
- question.innerHTML = questions.question;
- questionReplay.innerHTML = questions.question;
- questionReplayRight.innerHTML = questions.question;
- for (let j = 0; j < questions.answers.length; j++) {
- document.getElementById(j + 1).style.display = 'block';
- document.getElementById(j + 1).innerHTML = questions.answers[j];
- // !!! Registrieren des Eventlisteners hier gelöscht
- // und an das Ende des Skripts verschoben
- }
- });
- }
- getCountRows(makeQuestion);
- // // Die Anzahl der Fragen für den Prolog:
- // function makeProlog(countOfQuestions) {
- // countQuestions.innerHTML = countOfQuestions;
- // }
- // getCountRows(makeProlog);
- // // Und hier der restliche Code:
- // start.addEventListener('click', () => {
- // makeQuestion()
- // questionAnswers.style.cssText = 'display: grid; opacity: 1; transform: rotateY(0); transition: opacity 3s .3s ease-in, transform 3s 1s ease-in';
- // prologue.style.top = '-' + window.innerHeight + 'px';
- // prologue.style.transition = 'top 6s';
- // setTimeout(function () {
- // startTimer();
- // },
- // 4000);
- // });
- makeItBetterOne.addEventListener('click', () => {
- redOverlayTransform();
- answerBlockReScale();
- pointerEventsOn();
- w++;
- setTimeout(function () {
- startTimer();
- },
- 1000);
- });
- makeItBetterTwo.addEventListener('click', () => {
- redOverlayTransform();
- answerBlockReScale();
- pointerEventsOn();
- w++;
- setTimeout(function () {
- startTimer();
- },
- 1000);
- });
- makeItBetterThree.addEventListener('click', () => {
- redOverlayTransform();
- answerBlockReScale();
- pointerEventsOn();
- w++;
- setTimeout(function () {
- startTimer();
- },
- 1000);
- });
- makeItBetterFour.addEventListener('click', () => {
- redOverlayTransform();
- answerBlockReScale();
- pointerEventsOn();
- w++;
- setTimeout(function () {
- startTimer();
- },
- 1000);
- });
- next.addEventListener('click', () => {
- i++;
- makeQuestion();
- resetTimer();
- pointerEventsOn();
- playpart.style.cssText = 'opacity: 1; transition: opacity 2s';
- setTimeout(function () {
- startTimer();
- },
- 1000);
- greenOverlayTransform();
- answerBlockReScale();
- });
- againFromStart.addEventListener('click', () => {
- doItAgain()
- pointerEventsOn();
- next.style.display = 'block';
- noMoreQuestions.style.display = 'none';
- answerBlockReScale();
- setTimeout(function () {
- startTimer();
- },
- 4000);
- })
- againAfterTimeout.addEventListener('click', () => {
- doItAgain()
- pointerEventsOn();
- answerBlockReScale();
- setTimeout(function () {
- startTimer();
- },
- 4000);
- })
- // !!! Eventlisteners für Klick auf die Antworten registrieren.
- for (let j = 0; j < answerBtns.length; j++) {
- document.getElementById(j + 1).addEventListener('click', () => {
- pointerEventsOff()
- const answerNumber = document.getElementById(j + 1).getAttribute('id');
- // !!! An dieser Stelle steht uns die Antwort vom Server nicht mehr direkt zu Verfügung.
- // !!! Wir müssen sie daher aus dem HTML des Buttons holen:
- const answerTxt = answerBtns[j].innerHTML;
- answerReplay.innerHTML = answerTxt;
- answerReplayRight.innerHTML = answerTxt;
- // Prüft die Richtigkeit der gegebenen Antwort
- const trueNumber = new FormData();
- trueNumber.append('id', i);
- trueNumber.append('choosen', answerNumber);
- fetch('checkAnswer.php', {
- method: 'post',
- body: trueNumber
- }).then(rightInt => {
- return rightInt.json();
- }).then(rightInt => {
- if (rightInt.truenumber == true) {
- rightAnswerBlock()
- wrongOne.style.display = 'none';
- wrongTwo.style.display = 'none';
- wrongThree.style.display = 'none';
- wrongFour.style.display = 'none';
- points.innerHTML = rightInt.points;
- stopTimer()
- w = 1;
- if (i == countofquestions) {
- pointsEnd.innerHTML = rightInt.points;
- next.style.display = 'none';
- noMoreQuestions.style.display = 'block';
- stopTimer()
- questionAnswers.style.cssText = 'transform: rotate(0); opacity: 0; transition: opacity 7s 2s';
- finalOverlay.style.cssText = 'display: grid';
- finalOverlay.style.top = '-' + window.innerHeight + 'px';
- setTimeout(function () {
- finalOverlay.style.cssText = 'display: grid; top: 0; transition: top 3s ease-in';
- },
- 4000);
- }
- } else {
- if (w === 1) {
- wrongAnswerBlock()
- wrongOne.style.display = 'block';
- wrongTwo.style.display = 'none';
- wrongThree.style.display = 'none';
- wrongFour.style.display = 'none';
- }
- if (w === 2) {
- wrongAnswerBlock()
- wrongOne.style.display = 'none';
- wrongTwo.style.display = 'block';
- wrongThree.style.display = 'none';
- wrongFour.style.display = 'none';
- }
- if (w === 3) {
- wrongAnswerBlock()
- wrongOne.style.display = 'none';
- wrongTwo.style.display = 'none';
- wrongThree.style.display = 'block';
- wrongFour.style.display = 'none';
- }
- if (w >= 4) {
- wrongAnswerBlock()
- wrongOne.style.display = 'none';
- wrongTwo.style.display = 'none';
- wrongThree.style.display = 'none';
- wrongFour.style.display = 'block';
- }
- }
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement