Paul, I have done it using a sequence of setTimeout’s
It doest want to run in a codepen, only as a web page.
First the html, I changed it slightly.
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demonstration for use of async code</title>
<link href="asyncdemo.css" rel="stylesheet" type="text/css">
<script defer src="asyncDemo.js"></script>
</head>
<body id = 'body' data-tgtpid = "0" data-db-filename = "" data-rowcount = 0 data-mode = "reader" data-gender = "male">
<div id="content">
<header id="page-header">
<h1>Ready, Set, Go</h1>
</header>
<!-- could be a main content section -->
<section id="timing">
<header class="section-header">
<h2>Staging Duration (seconds)</h2>
</header>
<article class="article1">
<div id = "buttons" class="durationarray">
<div class="but durationcell"><input id="but5" type="button" value="5"></div>
<div class="but durationcell"><input id="but10" type="button" value="10"></div>
<div class="but durationcell"><input id="but15" type="button" value="15"></div>
</div>
</article>
</section>
<section id="lights">
<article class="article1">
<div id="colorwindow"><h1 id="countdown"><span id="timer"></span></h1></div>
</article>
</section>
<!---->
<footer id="footer">
<input id="resetbut" type="button" value="Start Over" class="but resetbut">
</footer>
</div>
</body>
</html>
Now the css, also changed slightly.
the colour of the numbers and the window colours as a class.
html {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing: inherit;
}
body {
font-family: arial, sans-serif;
margin: 0;
padding: 0;
background-color: black;
}
#content {
margin: 0;
padding: 1em;
}
input, button, reset {
font: inherit;
}
p, h1, h2, h3, h4, h5, h6 {
color: white;
}
.durationarray {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 100%;
gap: 8px;
justify-items: stretch;
height: 5em;
}
.durationcell {
justify-self: center;
align-self: center;
text-align: center;
width: 100%;
background-color: #666;
}
input[type="button"],
.but {
display:block;
border-radius: 5px;
font-weight: bold;
font-size: 2em;
color: white;
width: 100%;
background-color: #666;
}
#resetbut {
font-size: 3em;
}
#timer {
display: block;
margin:auto;
padding-top:40px;
color: red;
text-align:center;
font-size: 12em;
}
#colorwindow {
width: 100%;
height: 400px;
margin: 1em 0;
padding: 1em 2em 1em 1em;
}
.black {
background-color: black;
}
.yellow {
background-color: yellow;
}
.green {
background-color: green;
}
And finally the js
I used a click event rather than a submit event to start the sequence.
"use strict";
const $id = id => document.getElementById(id);
const min = 1;
const max = 7;
let delayA = ""
let delayB = "";
let delayC = ""
let div = $id('colorwindow');
const setDelays = ev => {
let randNr = Math.floor(Math.random() * (max - min) + min);
let tgt = ev.target;
delayA = ev.target.value;
delayB = +randNr;
delayC = 5;
action();
}
const showCountDown = ticks => {
let el = $id('timer');
if(ticks < 1) {
el.innerHTML = "";
} else {
el.innerHTML = ticks;
let tickCounter = () => {
if(ticksLeft < 1) {
clearTimeout(timerTick);
el.innerHTML = "";
} else {
el.innerHTML = ticksLeft - 1;
ticksLeft--;
}
}
let ticksLeft = ticks;
let timerTick = setInterval(tickCounter, 1000);
}
}
const action = () => {
console.log(`Delays are: ${delayA}, ${delayB} and ${delayC}`);
$id("buttons"),addEventListener('click', setDelays, false);
$id('colorwindow').classList.add("black");
showCountDown(delayA);
setTimeout(() => {
showCountDown(delayB);
console.log(`Changing to yellow after ${delayA} Secs` );
div.classList.remove("black");
div.classList.add("yellow");
setTimeout(() => {
showCountDown(delayC);
console.log(`Changing to green after ${delayB} Secs` );
div.classList.remove("yellow");
div.classList.add("green")
setTimeout(() => {
showCountDown(-1);
console.log(`Changing to black after ${delayC} Secs` );
div.classList.remove("green");
div.classList.add("black")},
delayC * 1000);
},
delayB * 1000);
},
delayA * 1000);
}
$id("buttons").addEventListener('click', setDelays, false);
The filenames are asyncDemo,html, asyncDemo.css and asyncDemo.js