With a collection of 5000 text strings, each assigned a time value ranging from 0 to 86400 seconds...
I am looking to determine the user's current time and display the corresponding text string. Subsequently, as the user's time changes, I aim to update the displayed text to match the next sequential text string and continue this pattern.
Being relatively new to JavaScript, my initial idea involves utilizing two arrays – one to store the text strings and another for the associated times. The code would then locate the appropriate time from the 'time' array and reveal the text string with the matching index from the other array. In scenarios where the user's current time falls between two values, the lower value should be chosen.
Here is an example:
var interval = [0, 1728, 3456, 5184, 6912, 8640, ...]; // Array holding time intervals
var textstring = ["Verse 1", "Verse 2", "Verse 3", ...]; // Array containing text strings
function displayverse() {
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
var currenttime = (hour * 3600) + (minute * 60) + second;
for (var i = 0; i < interval.length; i++) {
if (currenttime >= interval[i] && currenttime < interval[i + 1]) {
document.getElementById('verse').innerHTML = textstring[i];
}
}
}
My current implementation seems to have issues, especially with the 'if' statement. Could someone provide insights on what could be wrong?
Current Code: https://jsfiddle.net/wrgt1/9bs4eu5q/1/