I want to create a periodic process with inner cycles, using javascript. Each cycle is represented by consequently changing circles – with specified time interval between them and specified time interval between cycles. The problem is also getting harder, because I also need a variable time interval between cycles and circle (which I’m going to do using random number function).
Currently I have the following code, which doesn’t work properly:
<html>
<body>
<canvas id="canv" widht="800" height="500"></canvas>
<script>
var ctx=document.getElementById("canv").getContext('2d');
var cnt=0;
var int0=self.setInterval(function(){startDraw()},5000);
function startDraw ()
{
var int1=self.setInterval(function(){DrawC()},1000);
cnt++;
if (cnt==3)
{
int0=window.clearInterval(int0);
}
}
function DrawC()
{
ctx.beginPath();
ctx.arc(200, 200, 50, 0, 2*Math.PI, true);
ctx.fillStyle="yellow";
ctx.fill();
ctx.closePath();
var int2=self.setInterval(function(){DrawGC()},1000);
int1=window.clearInterval(int1);
}
function DrawGC()
{
ctx.beginPath();
ctx.arc(200, 200, 50, 0, 2*Math.PI, true);
ctx.fillStyle="green";
ctx.fill();
ctx.closePath();
var int3=self.setInterval(function(){DrawWC()},1000);
int2=window.clearInterval(int2);
}
function DrawWC()
{
ctx.beginPath();
ctx.arc(200, 200, 52, 0, 2*Math.PI, true);
ctx.fillStyle="white";
ctx.fill();
ctx.closePath();
int3=window.clearInterval(int3);
}
function getRandomInt(min, max)
{
return Math.floor(Math.random() * (max - min + 1)) + min;
}
</script>
</form>
</body>
</html>
You should make the timer variables as global. Try:
Remove these variable declarations from the functions.
Try out this code, is this what you need?