diff --git a/src/public/script.js b/src/public/script.js index 8dbd14c..493ef65 100644 --- a/src/public/script.js +++ b/src/public/script.js @@ -330,13 +330,11 @@ function modTimerToggle(){ if(modTimerRunning){ clearInterval(modTimerInterval);modTimerRunning=false; document.getElementById('btn-timer-ss').textContent='START'; - document.getElementById('timer-tog').checked=false; broadcastTimerToPlayers(modTimerRemaining,false); } else { if(modTimerRemaining<=0)modTimerLoad(); modTimerRunning=true; document.getElementById('btn-timer-ss').textContent='PAUSE'; - document.getElementById('timer-tog').checked=true; broadcastTimerToPlayers(modTimerRemaining,true); modTimerInterval=setInterval(()=>{ modTimerRemaining--; @@ -345,7 +343,6 @@ function modTimerToggle(){ if(modTimerRemaining<=0){ clearInterval(modTimerInterval);modTimerRunning=false; document.getElementById('btn-timer-ss').textContent='START'; - document.getElementById('timer-tog').checked=false; ws_send({type:'close_round'}); toast('TIME UP — round closed','warn'); if(typeof gsap!=='undefined'){ @@ -354,6 +351,9 @@ function modTimerToggle(){ } },1000); } + // Update toggle state to match running state + const tog=document.getElementById('timer-tog'); + if(tog)tog.checked=modTimerRunning; } function toggleTimerFromSwitch(){ @@ -373,14 +373,12 @@ function modTimerLoad(){ modTimerRunning=false; clearInterval(modTimerInterval); document.getElementById('btn-timer-ss').textContent='START'; - // Keep toggle state as is - don't auto-uncheck renderModTimerDisplay(); } function modTimerReset(){ clearInterval(modTimerInterval); modTimerRunning=false; - // Keep toggle state as is - don't auto-uncheck modTimerRemaining=Math.max(5,parseInt(document.getElementById('mod-timer-set').value)||30); renderModTimerDisplay(); broadcastTimerToPlayers(modTimerRemaining,false); @@ -400,15 +398,62 @@ function broadcastTimerToPlayers(sec,running){ function toggleRound(){ if(room.buzzerState.roundOpen){ ws_send({type:'close_round'}); + // If timer toggle is ON and timer running, pause it + if(document.getElementById('timer-tog')?.checked && modTimerRunning){ + clearInterval(modTimerInterval);modTimerRunning=false; + document.getElementById('btn-timer-ss').textContent='START'; + } } else { ws_send({type:'open_round'}); + // If timer toggle is ON and timer not running, start it + if(document.getElementById('timer-tog')?.checked && !modTimerRunning && modTimerRemaining===0){ + modTimerLoad(); + modTimerRunning=true; + document.getElementById('btn-timer-ss').textContent='PAUSE'; + modTimerInterval=setInterval(()=>{ + modTimerRemaining--; + renderModTimerDisplay(); + broadcastTimerToPlayers(modTimerRemaining,true); + if(modTimerRemaining<=0){ + clearInterval(modTimerInterval);modTimerRunning=false; + document.getElementById('btn-timer-ss').textContent='START'; + ws_send({type:'close_round'}); + toast('TIME UP — round closed','warn'); + } + },1000); + } } } function resumeRound(){ ws_send({type:'resume_round'}); + // If timer toggle is ON and timer not running, resume it + if(document.getElementById('timer-tog')?.checked && !modTimerRunning){ + modTimerLoad(); + modTimerRunning=true; + document.getElementById('btn-timer-ss').textContent='PAUSE'; + modTimerInterval=setInterval(()=>{ + modTimerRemaining--; + renderModTimerDisplay(); + broadcastTimerToPlayers(modTimerRemaining,true); + if(modTimerRemaining<=0){ + clearInterval(modTimerInterval);modTimerRunning=false; + document.getElementById('btn-timer-ss').textContent='START'; + ws_send({type:'close_round'}); + toast('TIME UP — round closed','warn'); + } + },1000); + } } +// Timer toggle handler +document.getElementById('timer-tog').addEventListener('change',function(e){ + if(e.target.checked && modTimerRemaining===0){ + // If timer is enabled but not started, load it from set value + modTimerLoad(); + } +}); + function renderRoundButtons(){ const btn=document.getElementById('mod-round-btn'); const resumeBtn=document.getElementById('mod-resume-btn');