countdown in javascript

1

When it comes to the "end" and I want it to change color, can you tell me how to do it?

<!DOCTYPE html>
<html>
<head>
  <title>javascript</title>
  <meta charset="utf-8">
  <link type="text/css" rel="stylesheet" media="screen" href="style.css" />
  <script type="text/javascript">

  var count = new Number();
  var count = 60;

  function start(){

    if ((count - 1) >= 0){
      count -= 1;
      if (count == 0) {
        count = "Atualizado";
      }else if(count < 10){
        count = "0"+count;
      }
      tempo.innerText=count;
      setTimeout('start();', 100);

    }
  }

  </script>

</head>
  <body onload="start();">
    <div id="tempo" ></div>
  </body>
</html>
    
asked by anonymous 13.06.2018 / 17:00

1 answer

1

You just need to change your javascript a little bit

var count = Number();
var count = 60;
var tempo = document.getElementById("tempo"); // associar a variavel tempo ao elemento

function start(){
     if ((count - 1) >= 0){
        count -= 1;
        if (count == 0) {
            count = "Atualizado";
        }else if(count < 10){
            count = "0"+count;
        }
        tempo.innerText=count;
        setTimeout(start, 100); 
        // em vez de chamar setTimeout("start();", 100) usa só o nome da funcao
        // o setTimeout vai executar a funcao mesmo sem pores os ()
    }
}

start(); // chamar a funcao start() a primeira vez

I made a JsFiddle where you can see the countdown to work

    
13.06.2018 / 17:08