Cycling/changing message using JS

Joined
Feb 16, 2023
Messages
1
Reaction score
0
Hi!
I have been working on a site for fun and I wanted to make a "cycling message". What I mean by that is a list of pre-written sentences that cycle every hour. I tried to find a guide online, but I can't. Any help would be appreciated!
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
We will use a simple array to hold all 24 messages. Then we'll find the time. Well, we'll find the day hour minute second year of the user's computer. We will do this when the user logs in to the site. NOTE: If they stay on site for longer than an hour the message does not change. The time we get is a 24-hour clock.
I am lazy so you need to expand the array for the rest of the messages.
Code:
<body>
<p id="time"></p>
<script>
const sayings = [];
sayings[0] = "message 1";
sayings[1] = "message 2";
sayings[2] = "message 3";
sayings[3] = "message 4";
sayings[4] = "message 5";
sayings[5] = "message 6";
sayings[6] = "message 7";

const date = new Date();
let hour = date.getHours();
document.getElementById("time").innerHTML = sayings[hour];
</script>
</body>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top