I'm trying to have a program that displays a sertain set of text depending on what time it is. It'll only show the last line but never any of the others, no matter what I try.
I am just wanting a simple solution. What I have done is probably very incorrect but I've tried my hardest. Here's what I have so far;
function I() {
setTimeout(M, 1)
document.getElementById("time").innerHTML = Date()
}
function M() {
setTimeout(I, 1);
var first = "8:00:00";
if (Date() > first) {
document.getElementByTag("p").innerHTML = "Hello!"
You are comparing a Date
object against a string, you need to compare it against another data object, the following snippet should work as expected.
You can learn more about the data object here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
function updateText() {
var currentTime = new Date();
var first = new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate(), 8, 0, 0);
var targetElement = document.getElementById("time");
if (currentTime > first) {
targetElement.innerHTML = "Hello!";
} else {
targetElement.innerHTML = "Not time yet!";
}
setTimeout(updateText, 1000);
}
updateText();
<p id="time"></p>