I'm trying to write a "Simon Says" game program with Jquery and CSS and I've come across a problem. I can't seem to make an element "flash" more than once.
I've isolated the problem into a simple model (code below), which calls the flash
function recursively, similarly to in this working version: https://codepen.io/nuo/pen/DaEkq
I've tried all sorts of approaches, including CSS and Jquery animations. The crux of the matter seems to be that the second CSS change overrides the first, so only one of them happens.
what am I missing here please?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flash</title>
<style>
#wrapper{
background-color: red;
opacity: 0.6;
width: 400px;
height: 400px;
margin: auto;
}
</style>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
</head>
<body>
<div id="wrapper"></div>
<script>
function flash(n){
console.log(n);
if(n > 0){
$('#wrapper').css('opacity', '1')
setTimeout(function(){
$('#wrapper').css('opacity', '0.6')
}, 1000);
flash(n-1);
}
}
flash(4);
</script>
</body>
</html>
flash(n-1)
run? When do you actually want it to run? (Answers: it currently runs immediately. You want it to run when the flash has ended, ie. inside thesetTimeout
.)