I am finding in JS that components that work OK then don’t when combined with other components do not anymore. The functions fruitPick() and timeRandom() work when on their own, but when inserted into the rest of this code it no longer has a delay.
fruitPick() selects a random fruit, that works. But the rand in timeRandom() does not. What is supposed to happen is at intervals of between 0 to 5 seconds a fruit is selected. Instead it picks just one fruit, then stops.
“`
<html>
<body bgcolor=cyan>
<img src =”animated_rest.gif” id=”animated_rest” width=”310″ height=”392″ style=”position: absolute; top:92px; left:375px; visibility:hidden”/>
<img src =”animated.gif” id=”animated” width=”310″ height=”392″ style=”position: absolute; top:92px; left:375px; visibility:hidden”/>
<img src =”animated_reverse.gif” id=”animated” width=”310″ height=”392″ style=”position: absolute; top:92px; left:375px; visibility:hidden”/>
<input type=”button” onclick=”not_animated()” value=”Not Animated”>
<input type=”button” onclick=”animated()” value=”Animated”>
<script type=”text/javascript”>
var input;
function fruitPick() {
input = [‘apple’, ‘orange’, ‘grape’, ‘peach jelly sandwich’][Math.floor(Math.random() * 4)];
inputLen = input.length;
}
function timeRandom() {
var min = 0;
var max = 5;
var rand = Math.floor(Math.random() * (max – min + 1) + min); //Generate Random number between 0 – 5
setTimeout(timeRandom, rand * 1000);
fruitPick();
}
function not_animated() {
document.getElementById(‘animated_rest’).style.visibility=’visible’;
document.getElementById(‘animated’).style.visibility=’hidden’;
}
function animated() {
timeRandom() //Call to random array string pick
if (randomImage= 2) {
document.getElementById(‘animated’).style.visibility=’visible’;
} else {
document.getElementById(‘animated_reverse’).style.visibility=’visible’;
}
speechCall();
var timeCount;
switch (true) {
case (inputLen < 6):
timeCount=350;
break;
case (inputLen < 12):
timeCount=600;
break;
case (inputLen <25):
timeCount=1200;
break;
case (inputLen <40):
timeCount=1800;
break;
case (inputLen < 50):
timeCount=2400;
break;
}
setTimeout(“talkStop()”, timeCount);
}
function talkStop() {
document.getElementById(‘animated_rest’).style.visibility=’visible’;
document.getElementById(‘animated’).style.visibility=’hidden’;
}
function speechCall() {
let msg = (input);
let speech = new SpeechSynthesisUtterance();
speech.lang = “en-US”; // Text Language
speech.text = msg; // The text you want to convert to speech
speech.volume = 1; // Volume
speech.rate = 1; // Speed
speech.pitch = 1; // Pitch
window.speechSynthesis.speak(speech);
}
</script>
</body>
</html>