2
People, I am creating a countdown timer accompanied by the buttons (start, pause, reset, restart and save time) and the initial time is set in an input as well.
I searched several times in various posts on pt.stackoverflow and other internet forums, I found at most regressive chronometers, but not with the same functionalities and although I tried to adapt I could not. In our forum we only find similar questions, but the language is not the same and when it is (pure js), the purpose is very different.
My code is with counting problem, I have noticed problems when it is more than 10 minutes or less than 1. I would like a resolution in Pure JS! Observe the code:
Javascript
function transporta(mensagem){
var docsetmin = document.getElementById('setminutos');
var docsetsec = document.getElementById('setsegundos');
var docrestamin = document.getElementById('restamin');
var docrestasec = document.getElementById('restasec');
if(docsetsec.value <= 59){
docrestamin.value = docsetmin.value;
docrestasec.value = docsetsec.value;
setTimeout(cronometro, 1000, 'start');
}
else{
alert('Há problemas com o tempo configurado!');
}
}
function cronometro(option, message){
var docsetmin = document.getElementById('setminutos');
var docsetsec = document.getElementById('setsegundos');
var docrestamin = document.getElementById('restamin');
var docrestasec = document.getElementById('restasec');
var docsetminval = docsetmin.value;
var docsetsecval = docsetsec.value;
var docrestaminval = docrestamin.value;
var docrestasecval = docrestasec.value;
if(option == 'start'){
start(message);
}
if(option == 'pause'){
docrestamin.placeholder = docrestamin.value;
docrestasec.placeholder = docrestasec.value;
alert('Cronômetro Pausado');
docrestamin.value = '--';
docrestasec.value = '--';
document.getElementById('paused').innerHTML = 'Pausado em: ' + docrestamin.placeholder + ':' + docrestasec.placeholder + '.';
}
if(option == 'reset'){
docsetmin.value = '00';
docsetsec.value = '00';
docrestamin.value = '00';
docrestasec.value = '00';
docrestamin.placeholder = '';
docrestasec.placeholder = '';
docsetmin.placeholder = '';
docsetsec.placeholder = '';
}
if(option == 'resume'){
document.getElementById('paused').innerHTML = '';
docrestamin.value = docrestamin.placeholder;
docrestasec.value = docrestasec.placeholder;
cronometro('start', message);
}
if(option == 'save'){
alert(docsetmin.value + ':' + docsetsec.value);
}
}
function start(alerta){
var docsetmin = document.getElementById('setminutos');
var docsetsec = document.getElementById('setsegundos');
var docrestamin = document.getElementById('restamin');
var docrestasec = document.getElementById('restasec');
/* Se for mais que 10 minutos */
if(docrestamin.value > 10){
/* Se for mais que 10 segundos */
if((docrestasec.value <= 59) && (docrestasec.value > 10)){
docrestasec.value = docrestasec.value - 1;
setTimeout(start, 1000);
}
/* Se for menor ou igual a 10 segundos e mais que 0 segundos */
if((docrestasec.value <= 10) && (docrestasec.value > 0)){
var ts = docrestasec.value - 1;
docrestasec.value = '0' + ts;
setTimeout(start, 1000, 'start');
}
/* Se for menor ou igual a 0 segundos */
if(docrestasec.value <= 0){
docrestasec.value = '59';
docrestamin.value = docrestamin.value - 1;
setTimeout(start, 1000, 'start');
}
}
/* Se for menor ou igual a 10 minutos e mais que 0 minutos */
if((docrestamin.value <= 10) && (docrestamin.value > 0)){
/* Se for mais que 10 segundos */
if((docrestasec.value > 10) && (docrestasec.value <= 59)){
docrestasec.value = docrestasec.value - 1;
setTimeout(start, 1000, 'start');
}
/* Se for menor ou igual a 10 segundos e mais que 0 segundos */
if(docrestasec.value <= 10 && docrestasec.value > 0){
var ts = docrestasec.value - 1;
docrestasec.value = '0' + ts;
setTimeout(start, 1000, 'start');
}
/* Se for menor ou igual a 0 segundos */
if(docrestasec.value <= 0){
var tm = docrestamin.value - 1;
docrestamin.value = '0' + tm;
docrestasec.value = 59;
setTimeout(start, 1000, 'start');
}
}
/* Se for menor ou igual a 0 minutos */
if(docrestamin.value <= 0){
/* Se for mais que 10 segundos */
if((docrestasec.value <= 59) && (docrestasec.value > 10)){
docrestasec.value = docrestasec.value - 1;
setTimeout(start, 1000, 'start');
}
/* Se for menor ou igual a 10 segundos e mais que 0 segundos */
if((docrestasec.value <= 10) && (docrestasec.value > 0)){
var ts = docrestasec.value - 1;
docrestasec.value = '0' + ts;
setTimeout(start, 1000, 'start');
}
/* Se for menor ou igual a 0 segundos */
if(docrestasec.value <= 0){
if((docrestamin.value = '00') && (docrestasec.value = '00')){
alert('O tempo acabou!');
}
}
}
}
function Onlynumbers(){
var tecla = event.keyCode;
if((tecla >= "48") && (tecla <= "57") && (tecla = "186")){
return true;
}
else{
return false;
}
}
CSS3
body{padding: 20%;}
header{text-align: center;}
section{align-items: center; justify-content: center; display: flex; vertical-align: middle;}
.btn{color: black; border: none; background-color: white; margin-left: 0.5%; margin-right: 0.5%; text-align: center;}
.border{border: 1px solid black;}
.border{transition: background-color 0.4s ease-in 0.2s; transition: color 0.01s ease-in;}
.border:hover{background-color: #1397D4; color: white;}
HTML
<!-- Cronômetro Regressivo -->
<header>
<p><h1>Cronômetro Regressivo</h1></p>
</header>
<br>
<section>
<input placeholder="m" class="btn" id="setminutos" type="text" name="minutos" size="2" maxlength="2" onkeypress="return Onlynumbers()">
:
<input placeholder="s" class="btn" id="setsegundos" maxlength="2" type="text" name="segundos" size="2" onkeypress="return Onlynumbers()">
<button id="comece" class="btn border" type="button" onclick="transporta()">Iniciar</button>
<button class="btn border" onclick="cronometro('pause');">Pausar</button>
<button class="btn border" onclick="cronometro('reset')">Resetar</button>
<button class="btn border" onclick="cronometro('resume')">Reiniciar</button>
<button class="btn border" onclick="cronometro('save')">Salvar Tempo</button>
<input class="btn dnone" id="restamin" type="text" name="minutosresta" size="2" maxlength="2" onkeypress="return Onlynumbers()">
<div class="dnone">:</div>
<input class="btn dnone" id="restasec" maxlength="2" type="text" name="segundosresta" size="2" onkeypress="return Onlynumbers()">
<div id="paused" class="dnone"></div>
</section>
Hello Saul! Thank you so much for helping me... I was desperate, I started from 0 with the switch and it worked! I really liked your logic! If possible, can you explain the clearTimeout and Parsefloat? Note: The input #showValue has to be size 5, so the time can be viewed completely... Very obg! Saved a lot!
– Giancarlo Soldera Graziano
clearTimeout cancels setTimeout. I put setTimeout in a global variable to cancel with clearTimeout. Parsefloat() takes the first characters separated by space and converts them into a float (real number). The ideal is to count in milliseconds and this count in milliseconds change the second and the second change the minute. In the example I made, I put the second to change the minute. I say this because if it pauses, there will be a small lag when returning but as I said, it is solved by placing in milliseconds. Hug
– Saulo Souza