2012-09-07 104 views

回答

0

您需要以類似currentY的方式添加currentX。然後,而不是遞增currentY你應該增加currentX。如果currentX不小於xnumHigh,則將其重新設置爲xnumLow並且增量爲currentY

基本上,你只是在做與y放在計時器上完全相同的東西,而不是x

+0

這裏是我的理解,並從你的答案做出來:http://jsfiddle.net/hnFse /? – Karem

+0

http://jsfiddle.net/hnFse/1/的作品。 –

0

下面是做這件事:(畫布的東西僅僅是一種視覺表現)

http://jsfiddle.net/J5GvM/1/

(function nestedLoop(){ 
    var innerStep = 10; 
    var xnumLow = 0; 
    var xnumHigh = 500; 
    var currentX = xnumLow; 

    var outerStep = 3000; 
    var ynumLow = 0; 
    var ynumHigh = 500; 
    var currentY = ynumLow; 

    var canvas = $("#canvas")[0]; 
    var ctx = canvas.getContext('2d'); 

    var drawPixel = function(x, y){ 
     ctx.fillRect(x,y,1,1); 
    }; 

    (function outerLoop(oStart, oEnd){ 
     currentX = xnumLow; 
     currentY = oStart; 

     (function innerLoop(iStart, iEnd){ 
      currentX = iStart; 

      // Do stuff here 
      drawPixel(currentX, currentY); 

      if (iStart < iEnd) 
       setTimeout(function(){innerLoop(++iStart, iEnd);}, innerStep); 
      else 
       setTimeout(function(){outerLoop(++oStart, oEnd);}, outerStep); 
     })(currentX, xnumHigh); 
    })(currentY, ynumHigh); 
})();​