luistavares

JavaScript - Criando um temporizador decrescente com JavaScript

Oct 31st, 2022
1,723
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.82 KB | Source Code | 0 0
  1. <html lang="pt-br">
  2. <head>
  3. <meta charset="utf-8" />
  4. <title>Cronômetro com Javascript</title>
  5. <style>
  6. *{
  7.     margin:0;
  8.     padding:0;
  9. }
  10. body{
  11.     background:#CCC;
  12.     color:#FFF;
  13.     font-family:Arial, Helvetica, sans-serif;
  14.     text-align:center;
  15. }
  16. #topo{
  17.     background:#069;
  18.     height:50px;   
  19.     border-bottom:2px solid #006;
  20.     padding-top: 10px;
  21. }
  22. h2 a{
  23.     color:#069;
  24.     text-decoration:none;
  25. }
  26. h2 a:hover{
  27.     color:#006;
  28. }
  29. input{
  30.     background:#ccc;
  31.     width:600px;
  32.     height:200px;
  33.     line-height:200px;
  34.     font-size:240px;
  35.     border:none;
  36. }
  37. #cro{
  38.     width: 600px;
  39.     clear:both;
  40.     margin-bottom: 20px;
  41.     margin-left: auto;
  42.     margin-right:auto;
  43. }
  44. .mybtn{
  45.     cursor: pointer;
  46.     font-size:18px;
  47.     clear:both;
  48.     margin-bottom: 6px;
  49.     padding: 10px;
  50.     border-radius: 10px;
  51.     width: 450px;
  52. }
  53. .container{
  54.     width:100%;
  55. }
  56. .zeb{
  57.     background-color: #88AAAA;
  58. }
  59. </style>
  60. <script type="text/javascript">
  61.     var tempo = 0;          // tempo em segundos
  62.     var working = false;    // sinaliza se o timer está ativado   
  63.     var intervaloID = 0;    // identificação do intervalo que permite limpá-lo
  64.  
  65.     function exibeTempo(){
  66.         let min = parseInt(tempo/60);   // Pega a parte inteira dos minutos    
  67.         let seg = tempo%60;             // Calcula os segundos restantes
  68.         let smin = min.toString().padStart(2, '0'); // Formata o número em duas casas
  69.         let sseg = seg.toString().padStart(2, '0');        
  70.        
  71.         let tempoTela = smin + ':' + sseg; // Variável para formatar no estilo cronômetro
  72.         document.querySelector(".cronometro").value = tempoTela;
  73.         tempo--;
  74.  
  75.         if (tempo < 0){ // zera temporizador
  76.             working = false;
  77.             clearInterval(intervaloID);
  78.             document.querySelector(".cronometro").style.backgroundColor = '#ccc';  
  79.         }
  80.         else if(tempo < 15){ // alerta dos 15 seg. finais
  81.             document.querySelector(".cronometro").style.backgroundColor = 'red';
  82.         }
  83.     }
  84.  
  85.     function temporizador(t){      
  86.         if (working == false){         
  87.             working = true;
  88.             tempo = t;
  89.             exibeTempo();
  90.             intervaloID = setInterval(exibeTempo, 1000); // 1s                 
  91.         }
  92.     }
  93. </script>
  94. </head>
  95. <body>
  96.     <div class='container'>
  97.         <div id="topo">
  98.             <h1>Debate Eleições 2022 - Cronômetro</h1>
  99.         </div>
  100.         <div id="cro">
  101.             <input type="text" class="cronometro" value="00:00" readonly/>
  102.         </div><br />
  103.         <button class='mybtn' onclick="temporizador(180)">3 minutos (Apresentação Inicial)</button><br />
  104.         <button class='mybtn zeb' onclick="temporizador(300)">5 minutos (Bloco perguntas, Considerações Finais)</button><br />
  105.         <button class='mybtn' onclick="temporizador(60)">1 minuto (Pergunta/Tréplica)</button><br />
  106.         <button class='mybtn zeb' onclick="temporizador(120)">2 minutos (Resposta/Réplica)</button><br />
  107.         <button class='mybtn' onclick="window.location='index.html';">Limpar</button>
  108.     </div>
  109. </body>
  110. </html>
  111.  
Advertisement
Comments
  • catz33
    3 years
    # text 0.10 KB | 0 0
    1. https://wtools.io/paste-code/bG1R
    2. https://wtools.io/paste-code/bG1S
    3. https://wtools.io/paste-code/bG1T
Add Comment
Please, Sign In to add comment