From 63f8e77faaa2e56dfbe331a17b64498389a4cea7 Mon Sep 17 00:00:00 2001 From: adplantade Date: Wed, 12 Aug 2020 10:09:08 +0200 Subject: [PATCH] pause fonctionnelle en mode "temps" et "survie" --- code/timer.js | 89 +++++++++++++++++++++++++++++++------------------ code/vitraux.js | 7 ++++ 2 files changed, 63 insertions(+), 33 deletions(-) diff --git a/code/timer.js b/code/timer.js index 4b9558b..4524d4c 100644 --- a/code/timer.js +++ b/code/timer.js @@ -1,33 +1,56 @@ -var fin = new Date(); -fin.setMinutes(fin.getMinutes()+tpsRem[1]); -document.getElementById("timer").innerHTML = tpsRem[0] + ":" + tpsRem[1] + ":" + tpsRem[2]; - -// Update the count down every 1 second -var x = setInterval(function() { - - // Get today's date and time - var now = new Date().getTime();; - - - // Find the distance between now and the count down date - var distance = fin - now; - - // Time calculations for days, hours, minutes and seconds - var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); - var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); - var seconds = Math.floor((distance % (1000 * 60)) / 1000); - - document.getElementById("timer").innerHTML = hours + ":" + minutes + ":" + seconds; - - // If the count down is over, write some text - if (distance < 0) { - clearInterval(x); - console.log("OUT"); - window.location.replace("./score.php?pts="+points+"&mode="+get['mode']+"&diff="+get['diff']+"&pseudo="+get['pseudo']); - } -}, 1000); - -function addTime(secs) -{ - fin.setSeconds(fin.getSeconds()+secs); -} \ No newline at end of file + +//--------------- +var time_in_minutes = tpsRem[1]; +var current_time = Date.parse(new Date()); +var deadline = new Date(current_time + time_in_minutes*60*1000); + + +function time_remaining(endtime){ + var t = Date.parse(endtime) - Date.parse(new Date()); + var seconds = Math.floor( (t/1000) % 60 ); + var minutes = Math.floor( (t/1000/60) % 60 ); + var hours = Math.floor( (t/(1000*60*60)) % 24 ); + var days = Math.floor( t/(1000*60*60*24) ); + return {'total':t, 'days':days, 'hours':hours, 'minutes':minutes, 'seconds':seconds}; +} + +var timeinterval; +function run_clock(id,endtime){ + var clock = document.getElementById(id); + function update_clock(){ + var t = time_remaining(endtime); + clock.innerHTML = t.hours + ":" + t.minutes + ":" + t.seconds; + if(t.total<=0) + { + clearInterval(timeinterval); + window.location.replace("./score.php?pts="+points+"&mode="+get['mode']+"&diff="+get['diff']+"&pseudo="+get['pseudo']); + } + } + update_clock(); // run function once at first to avoid delay + timeinterval = setInterval(update_clock,1000); +} +run_clock('timer',deadline); + + +var pausedC = false; // is the clock paused? +var time_left; // time left on the clock when paused + +function pause_clock(){ + if(!pausedC){ + pausedC = true; + clearInterval(timeinterval); // stop the clock + time_left = time_remaining(deadline).total; // preserve remaining time + } +} + +function resume_clock(){ + if(pausedC){ + pausedC = false; + + // update the deadline to preserve the amount of time remaining + deadline = new Date(Date.parse(new Date()) + time_left); + + // start the clock + run_clock('timer',deadline); + } +} diff --git a/code/vitraux.js b/code/vitraux.js index 1d81f94..f60c706 100644 --- a/code/vitraux.js +++ b/code/vitraux.js @@ -550,7 +550,10 @@ function checkAllOK(justChecking=false){ var audio = new Audio('ress/Jewel4.mp3'); audio.play(); if(pause) + { chronoStop(); + pause_clock(); + } handleScore(1); won=true; } @@ -696,7 +699,11 @@ function mdManager(e){ } if(paused) + { chronoContinue(); + resume_clock(); + } + document.getElementById(e.currentTarget.id).classList.add('vitrail-select'); }