Browser does not interpret javascript in the right sequence

Asked

Viewed 55 times

-4

o codigo Html

Aqui o codigo javascript

the javascript code Como sai no Browser

here in the Browser reads the right time but the img does not leave the correspontende being when I put, var time a time corresponding to the time leaves the right picture but when I leave to javascript sends to Browser leaves the last reading of javascript.

  • 1

    Post the code, not the code image that makes it easier for people to help you

  • In that code of yours, hora will be a string in format HH:MM. There’s no point comparing this string with numerical values like you’re doing in your code, which is why you’re always falling into else.

  • Great reasons not to use images when using text: https://pt.meta.stackoverflow.com/a/7817/3635

1 answer

1


You are comparing the variable "Time", a string, with an integer

if(hora > 0 && hora <=12) { ...} 

This will always fall into Isis, so it will always fall into the image of the night.

Do:

var hora = date.getHours();
if( hora >= 0 and hora < 12 ) { 
sua imagem manhã 
} else if (hora < 18) { 
sua imagem da tarde 
} else { sua imagem da noite }

With this, whenever you use the function "getHours()", it will bring you the whole hour, so just compare it, because 11:1 and 11:59 will have return 11.

  • I would like to bring the hour with minutes ,not just the hour ,I will try to do so using the and

  • @Evertoncostasouza, just save in a variable the hour with minutes to display on the screen and this variable only with the time to use as test for image.

  • ,thanks was show ,had forgotten it ,because I am learning React and there do not change so equal in javascript after declared.

  • It happens when we’re messing with some language and we need to do something in another. Just accept my answer, please, to help with the Stackoverflow profile. Hug

Browser other questions tagged

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