2013-01-10 285 views
0

我想使用javascript创建具有内部循环的周期性进程。每个周期由相应变化的圆圈表示 - 它们之间的指定时间间隔以及指定的周期之间的时间间隔。问题也越来越难,因为我还需要一个循环和循环之间的可变时间间隔(我将使用随机数函数)。循环使用javascript循环使用javascript

目前,我有下面的代码,它不能正常工作:使用此代码

<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> 
+3

你的问题到底是什么? –

回答

0

你应该让计时器变量作为全球。试试:

var int0, int1, int2, int3; 

从函数中删除这些变量声明。

试一试这段代码,这是你需要的吗?

<html> 
<body> 

<canvas id="canv" widht="800" height="500"></canvas> 
<script> 

var ctx=document.getElementById("canv").getContext('2d'); 
var cnt=0; 

var int0, int1, int2, int3; 

circleTime = 1000; 
cycleTime = 5000; 


int0 = self.setInterval(startDraw, cycleTime); 

function startDraw() 
{ 
    console.log("startDraw..."); 
    // Start drawing circles 
    self.setTimeout(DrawC, circleTime); 

    cnt++; 
    if (cnt == 4) 
    { 
     // Stop startDraw 
     int0 = window.clearInterval(int0); 

     /* 
     // Let's draw again 
     cnt = 0; 
     cycleTime = getRandomInt(5000, 10000); 
     circleTime = getRandomInt(1000, 2000); 
     int0 = self.setInterval(startDraw, cycleTime); 
     */ 
    } 
} 

function DrawC() 
{ 
    console.log("Circle..."); 

    ctx.beginPath(); 
    ctx.arc(200, 200, 50, 0, 2*Math.PI, true); 
    ctx.fillStyle="yellow"; 
    ctx.fill(); 
    ctx.closePath(); 

    self.setTimeout(DrawGC, circleTime); 
} 

function DrawGC() 
{ 
    console.log("greenCircle..."); 

    ctx.beginPath(); 
    ctx.arc(200, 200, 50, 0, 2*Math.PI, true); 
    ctx.fillStyle="green"; 
    ctx.fill(); 
    ctx.closePath(); 

    self.setTimeout(DrawWC, circleTime); 
} 

function DrawWC() 
{ 
    console.log("whiteCircle..."); 

    ctx.beginPath(); 
    ctx.arc(200, 200, 52, 0, 2*Math.PI, true); 
    ctx.fillStyle="white"; 
    ctx.fill(); 
    ctx.closePath(); 
} 

function getRandomInt(min, max) 
{ 
    return Math.floor(Math.random() * (max - min + 1)) + min; 
} 

</script> 
</form> 

</body> 
</html> 
+0

这几乎是我所需要的,但该过程在4个周期后不会停止... –

+0

何时应该停止? – ATOzTOA

+0

我将从文本框中获取周期数,但是,无论如何,在示例中让它为4个周期。 –

0

问题1: 您使用的,如果它是globaly定义(在DrawC定义)INT2,间隔将不会被清除,因为int2在DrawGC中没有值。 尝试定义它们全球 var int1, int2, int3;在你的代码

其次第一线,你为什么要使用的时间间隔,如果你想在1个循环取消它们,使用setTimeout()而不是只调用该方法一次

+0

setTimeout不是解决方案,因为,据我所知,虽然JS处理超时设置为相应的当前时间,所以,设置超时将无法在周期中正常工作(而不是秒周期间隔,我会得到毫秒间隔) 。 –