if loop ✍ with delay
basic script
<script type="text/javascript">
var loopNum=5;
function theLoop(loopNum) {
setTimeout(function() {
if (--loopNum)
{theLoop(loopNum)};
}, 1000);
showCount.innerHTML = ("number - " + loopNum);
} (loopNum);
</script>
var loopNum=5;
function theLoop(loopNum) {
setTimeout(function() {
if (--loopNum)
{theLoop(loopNum)};
}, 1000);
showCount.innerHTML = ("number - " + loopNum);
} (loopNum);
</script>
full code
<!doctype html>
<html lang="en">
<!-- scripting by Serge SEVEAU June 2023 -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>if & delay loop</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
#showCount {
width:25%;
height: 25px;
border: 1px solid #7d00d7;
border-radius: 7px;
font-size: 1.10em;
padding: 5px;
background-color: #faf9f9;
}
/*code by Serge Seveau June 2023*/
</style>
</head>
<body>
<div id="showCount"></div><br>
<input type="button" id="execute" value="click for count-down" onclick="theLoop(loopNum)"/>
<script type="text/javascript">
var loopNum=5; // this is the loop number
function theLoop(loopNum) {
setTimeout(function() {
//if (loopNum--) // if x > 0 then go again BUT includes 0
if (--loopNum) // if x > 0 then go again BUT not include 0
{theLoop(loopNum)}; // call the loop with allocated current value of x
}, 1000); // nominated time delay in milliseconds - 1000 is approx 1 second
showCount.innerHTML = ("number - " + loopNum);
} (loopNum); // number of times the loop runs
</script>
</body>
</html>
<html lang="en">
<!-- scripting by Serge SEVEAU June 2023 -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>if & delay loop</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
#showCount {
width:25%;
height: 25px;
border: 1px solid #7d00d7;
border-radius: 7px;
font-size: 1.10em;
padding: 5px;
background-color: #faf9f9;
}
/*code by Serge Seveau June 2023*/
</style>
</head>
<body>
<div id="showCount"></div><br>
<input type="button" id="execute" value="click for count-down" onclick="theLoop(loopNum)"/>
<script type="text/javascript">
var loopNum=5; // this is the loop number
function theLoop(loopNum) {
setTimeout(function() {
//if (loopNum--) // if x > 0 then go again BUT includes 0
if (--loopNum) // if x > 0 then go again BUT not include 0
{theLoop(loopNum)}; // call the loop with allocated current value of x
}, 1000); // nominated time delay in milliseconds - 1000 is approx 1 second
showCount.innerHTML = ("number - " + loopNum);
} (loopNum); // number of times the loop runs
</script>
</body>
</html>