html/javascript clock does not appear

Asked

Viewed 285 times

2

I’m trying to make a digital clock appear on the screen, but the browser does not return anything.

<!DOCTYPE html>
<html>
<head>
    <title>teste</title>
    <meta charset="utf-8">
    <script type="text/javascript">
        function Relogio{
            var data = new Data();
            var horas = data.getHours();
            var minutos = data.getminutes ();
            var segundos = data.getseconds();
                if (horas <10 ) {
                    horas= "0"+ horas;
                }
                if (minutos <10) {
                    minutos = "0"+ minutos;
                }
                if (segundos <10) {
                    minutos = "0"+segundos;
                }
    document.getElementByid("relogio").innerHTML =horas+":"+minutos+":"+segundos;
        }
    </script>

</head>
<body onload="relogio();">
</body>
</html>

3 answers

9

Always open your browser console to see errors! There are several syntax errors in your code:

  • new Data() instead of new Date().
  • getElementByid instead of getElementById.
  • function Relogio{ should be function Relogio(){ - or better still, function relogio() {
  • getminutes() should be getMinutes().

These errors appear in red on the console. You should even have other errors of the kind, see the comment below, Guilherme Nascimento, the other answers, and review your code! Anyway the open console helps you find all these bugs.

Then there is a logic problem: you try to catch an element with id "relogio", but there is no element with that ID. Switch to document.body and the clock will appear on the screen. In advance, your next challenge will be to make this watch go by itself, because the way you programmed it will always show the page load time.

  • 1

    Very well quoted the problems, also has a problem he wrote function Relogio{ when it should be function Relogio() { and getminutes should getMinutes, to sum up, there are many errors in the script, maybe even some that I haven’t noticed.

  • Really, I hadn’t seen it. I went to the ones that screamed the most. So I thought it was important to talk about the island.

  • I agree, so I said at the beginning, very well quoted the problems, I was referring to the console mainly, I just think the question should already be closed, it’s purely typo. If you don’t keep coming up with answers like Lucas would be there for comment.

3

Keep in mind that Javascript is case sensitive, that is, differentiates upper and lower case letters: a is different from A. Any character with the marry wrong cause error (syntax, can’t find an object etc.).

Problems encountered:

1. function Relogio{

After the function name should follow the parentheses: function Relogio(){

2. relógio() vs. Relogio()

Note that in the onload of body the function name is all lowercase, and the function name starts with uppercase "R". Either you change one or the other so that both are equal.

3. var data = new Data();

The syntax is incorrect. The correct is Date() (in English).

4. var minutos = data.getminutes ();

A problem of marry. Javascript usually uses the style Camel case when a property or method name is composed: get + minutes = getMinutes.

5. var segundos = data.getseconds();

Same problem as previous item.

6. document.getElementByid("relogio").innerHTML

Another problem of marry that causes syntax error. Remember the camel case? The correct is the id start with a capital "i": document.getElementById.


Only the element where you want to print the watch is missing. By .innerHTML of the code, is supposed to be a div with id #relogio. Insert it into the body:

<div id="relogio"></div>

If you want a timer so that the watch does not become static, use the setInterval() (uninterrupted timer that performs a function in the stipulated millisecond range).

How are you calling the function via onload, in this case, you can put the setInterval() at the end of the function relogio(), in this way:

setInterval(relogio, 1000); // 1000 = 1 segundo

Another important point (for beginners and experienced) is related to indentation code. A well-indented code makes it more readable and makes it much easier to maintain, especially in long codes (learn more at this link).


To study:

Corrected code:

function relogio(){
   var data = new Date();
   var horas = data.getHours();
   var minutos = data.getMinutes ();
   var segundos = data.getSeconds();
   if (horas <10 ) {
      horas= "0"+ horas;
   }
   if (minutos <10) {
      minutos = "0"+ minutos;
   }
   if (segundos <10) {
      minutos = "0"+segundos;
   }
   document.getElementById("relogio").innerHTML =horas+":"+minutos+":"+segundos;
   setInterval(relogio, 1000);
}
<body onload="relogio();">
   <div id="relogio"></div>
</body>

And finally: if you are using HTML5, you do not need to declare type="text/javascript" in the script.

2

Adjustments made

Missed them () for Function and put the lowercase r, as it is a best practice Function starting with lowercase letter and you are also calling in the onload with lowercase letter.

Clock()

To create the date object use Date()

var data = new Data(); for var data = new Date();

The name of the 2 functions of the Date object were wrong because javascript is case sensitive

var minutes = date.getminutes(); for var minutes = data.getMinutes();

var seconds = data.getseconds(); for var seconds = date getSeconds();

The name was incorrect because javascript is case sensitive

Document.getElementByid for Document.getElementById

I added the div with the time id to receive the time value.

<div id="relogio"> </div>

It follows code adjusted with what already had.

function relogio(){
  var data = new Date();
  var horas = data.getHours();
  var minutos = data.getMinutes();
  var segundos = data.getSeconds();
  
  if (horas <10 ) {
      horas= "0"+ horas;
  }
  
  if (minutos <10) {
      minutos = "0"+ minutos;
  }
  
  if (segundos <10) {
      minutos = "0"+segundos;
  }
  
  document.getElementById("relogio").innerHTML = horas+":"+minutos+":"+segundos;
}
<!DOCTYPE html>
<html>
<head>
<title>teste</title>
<meta charset="utf-8">
<script type="text/javascript">
    
</script>

</head>
<body onload="relogio();">
 <div id="relogio"> </div>
</body>
</html>

If you want to run every second you must use the setInterval as below.

setInterval(relogio, 1000);

You must insert before the line because it should be inside the script.

  • 2

    It would be nice if you explained what was wrong and what changes you made to make it work. You can [Edit] your question if you want to add these details. Incidentally, leave a comment letting me know if you make the edit =)

  • 1

    Good evening I did the editing with the adjustments made.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.