Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html lang="pt-br">
- <head>
- <meta charset="utf-8" />
- <title>Cronômetro com Javascript</title>
- <style>
- *{
- margin:0;
- padding:0;
- }
- body{
- background:#CCC;
- color:#FFF;
- font-family:Arial, Helvetica, sans-serif;
- text-align:center;
- }
- #topo{
- background:#069;
- height:50px;
- border-bottom:2px solid #006;
- padding-top: 10px;
- }
- h2 a{
- color:#069;
- text-decoration:none;
- }
- h2 a:hover{
- color:#006;
- }
- input{
- background:#ccc;
- width:600px;
- height:200px;
- line-height:200px;
- font-size:240px;
- border:none;
- }
- #cro{
- width: 600px;
- clear:both;
- margin-bottom: 20px;
- margin-left: auto;
- margin-right:auto;
- }
- .mybtn{
- cursor: pointer;
- font-size:18px;
- clear:both;
- margin-bottom: 6px;
- padding: 10px;
- border-radius: 10px;
- width: 450px;
- }
- .container{
- width:100%;
- }
- .zeb{
- background-color: #88AAAA;
- }
- </style>
- <script type="text/javascript">
- var tempo = 300; // tempo em segundos
- var working = false; // sinaliza se o timer está ativado
- function startCountdown(){
- let min = parseInt(tempo/60); // Pega a parte inteira dos minutos
- let seg = tempo%60; // Calcula os segundos restantes
- let smin = min.toString().padStart(2, '0') // Formata o número em duas casas
- let sseg = seg.toString().padStart(2, '0')
- let tempoTela = smin + ':' + sseg; // Variável para formatar no estilo cronômetro
- document.querySelector(".cronometro").value = tempoTela;
- if(tempo > 15){ // tempo maior que 15 segundos
- setTimeout(startCountdown, 1000); // espera 1s e chama novamente função
- tempo--; // decrementa o tempo
- }
- else if (tempo > 0){ // entra neste IF entre segundos 1 e 15
- document.querySelector(".cronometro").style.backgroundColor = 'red';
- setTimeout(startCountdown, 1000); // espera 1s e chama novamente função
- tempo--; // decrementa o tempo
- }
- else { // entra neste IF no segundo 0
- working = false;
- document.querySelector(".cronometro").style.backgroundColor = '#ccc';
- }
- }
- function setTempo(t){
- if (working == false){
- tempo = t;
- working = true;
- startCountdown();
- }
- }
- </script>
- </head>
- <body>
- <div class='container'>
- <div id="topo">
- <h1>Debate Eleições 2022 - Cronômetro</h1>
- </div>
- <div id="cro">
- <input type="text" class="cronometro" value="00:00" readonly/>
- </div><br />
- <button class='mybtn' onclick="setTempo(180)">3 minutos (Apresentação Inicial)</button><br />
- <button class='mybtn zeb' onclick="setTempo(300)">5 minutos (Bloco perguntas, Considerações Finais)</button><br />
- <button class='mybtn' onclick="setTempo(60)">1 minuto (Pergunta/Tréplica)</button><br />
- <button class='mybtn zeb' onclick="setTempo(120)">2 minutos (Resposta/Réplica)</button><br />
- <button class='mybtn' onclick="window.location='index.html';">Limpar</button>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment