-2
i have the following code in Javascript
function carregar() {
var msg = document.getElementById('msg')
var img = document.getElementById('imagem')
var data = new Date()
var hora = data.getHours()
var min = data.getMinutes()
var seg = data.getSeconds()
setTimeout('carregar()', 500)
msg.innerHTML = `Agora são ${hora}:${min}:${seg}.`
if (hora >= 0 && hora < 12) {
//BOM DIA!
img.src = 'amanhecer-redonda.png'
document.body.style.background = '#FAF1FA'
} else if (hora >= 12 && hora < 18) {
//BOA TARDE!
img.src = 'entardecer-redonda.png'
document.body.style.background = '#EFC529'
} else {
//BOA NOITE!
img.src = 'anoitecer-redonda.png'
document.body.style.background = '#4428FF'
}}
However, I would like to convert it to Jquery, as a way of studies. I am since yesterday trying, I got here. They could give me a light?
$(function carregar () {
var msg = $('#msg')
var img = $('#imagem')
var data = new Date()
var hora = data.getHours()
var min = data.getMinutes()
var seg = data.getSeconds()
setTimeout('carregar ()', 500).html(`Agora são ${hora}:${min}:${seg}.`)
if (hora >= 0 && hora < 12) {
//BOM DIA!
img.src = 'amanhecer-redonda.png'
document.body.style.background = '#FAF1FA'
} else if (hora >= 12 && hora < 18) {
//BOA TARDE!
img.src = 'entardecer-redonda.png'
document.body.style.background = '#EFC529'
} else {
//BOA NOITE!
img.src = 'anoitecer-redonda.png'
document.body.style.background = '#4428FF'
}
}}
$(function carregar () {
don’t need it, keep usingfunction carregar () {
. Put the points where there’s doubt, just put the code in the question is something like "look there and solve for me", you know? Comment on what you doubt, what you are not managing to change there yes we can help– Ricardo Pontual
Not directly related, but
getHours
returns a number between 0 and 23, then you don’t need to test if it’s greater than zero. It might just beif (hora < 12) { manhã } else if (hora < 18) { tarde } else { noite }
- In theelse if
also do not need to test if it is bigger than 12, because if it was not, would have entered in the firstif
(if it arrived in thiselse
is because it is definitely >= 12, so you only need to test if it is < 18).– hkotsubo
@hkotsubo I didn’t know that. Thank you so much for the teaching, it helps to make the code cleaner @Ricardopunctual the $ is because of that

$(document ).ready() 

this can be summarized with only one $.– Pablo Stefanes