I have the following code as part of an idea to teach my kid how to read time. I'm trying to take the output of the clock program and insert it into a list of incorrect times. The idea being when the correct time, in comparison to an analogue clock, shows to then say so.
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<style>
.clock {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 30px;
}
</style>
</head>
<body onLoad="startclock()">
<div id="changeTime" class="clock"></div>
<script type="text/javascript">
var timerID = null
var timerRunning = false
function stopclock() {
if (timerRunning)
clearTimeout(timerID)
timerRunning = false
}
function startclock() {
stopclock()
showtime()
}
function showtime() {
var now = new Date()
var hours = now.getHours()
var minutes = now.getMinutes()
var timeValue = "" + ((hours > 12) ? hours - 12 : hours)
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += (hours >= 12) ? " P.M." : " A.M."
document.getElementsByClassName("clock").value = timeValue
timerID = setTimeout("showtime()", 1000)
timerRunning = true
}
var myTime = ['08:07 A.M.', '10:15 A.M.', '03:43 P.M.', '07:44 P.M.', '11:59 A.M.', '01:19 P.M.', '01:21 A.M.'];
myTime.push(timerID);
var counter = 0;
var elem = document.getElementById("changeTime");
setInterval(change, 3000);
function change() {
var counter = Math.floor(Math.random() * myTime.length);
elem.innerHTML = myTime[counter];
}
</script>
</body>
</html>
I've tried using what I think is the correct variable and using push to insert it into the list however when the correct time is supposed to show it goes blank. My other problem is that when initially activated the first three seconds it is always blank. Any help fixing these two issues would be gratefully appreciated as I'm still very much learning javascript.
Please check www.testing12audio.com for what I have so far.