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.
Very well quoted the problems, also has a problem he wrote
function Relogio{
when it should befunction Relogio() {
andgetminutes
shouldgetMinutes
, to sum up, there are many errors in the script, maybe even some that I haven’t noticed.– Guilherme Nascimento
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.
– bfavaretto
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.
– Guilherme Nascimento