Javascript 恢复嵌套for循环

Javascript 恢复嵌套for循环,javascript,asynchronous,Javascript,Asynchronous,在javascript中对表格/像素图像进行循环时,我尝试异步进行,以防止浏览器延迟 因此,当执行一定数量的迭代时,我尝试退出并恢复for循环。这是一个嵌套循环,用于x和y坐标。这是我的理念: //`data` contains info from prewious executions function asynchronous(data) { var x = data.x; var y = data.y; var max_x = ...; var max_y

在javascript中对表格/像素图像进行循环时,我尝试异步进行,以防止浏览器延迟

因此,当执行一定数量的迭代时,我尝试退出并恢复for循环。这是一个嵌套循环,用于x和y坐标。这是我的理念:

//`data` contains info from prewious executions
function asynchronous(data) {
    var x = data.x;
    var y = data.y;
    var max_x = ...;
    var max_y = ...;
    var iterations = 0;
    var max_iterations = 1000;
    for(;y<max_y&&iterations<max_iterations; y++) {
      for(;x<max_x&&iterations<max_iterations; x++) {
        //Do something
        ...
        //Iterate iterations
        iterations++;
      }
    }
    //Print the status
    console.log(...);
    //Save our position for next execution
    data.x = x;
    data.y = y;
}
由于某些原因,变量没有正确重置,我得到了无限循环:

我有这方面的课:

  new ProgressManager(
    function() {
      //x and y from prewious iterations
      var x = this.x;
      var y= this.y;
      //Canvas related stuff
      var array = this.imageData.data;
      var width = this.imageData.width;
      var height = this.imageData.height;
      //step in this progress execution
      var step=0;
      //Iterate through x and y, starting from prewious values
      for (;y < height&&step<1000; y++) {
        //Cache computed y offset
        var ofy = y * width;
        for (;x < width&&step<1000; x++) {
          //Do some canvas operation here

          //Iterate step
          step++;
        }
      }
      this.x = x;
      this.y = y;
      this.progress+=step;
      return new ProgressEvent(null, null, variables.progress/(width*height),1);
    },
    //Data for the progress
    {x:0,y:0,progress:0,ctx:ctx,imageData:data},
    //onFinish callback
    function() {
      console.log("DONE!");
      this.ctx.putImageData(this.imageData);    
    },
    //onProgress callback
    function(event) {
      document.title=Math.round((event.progress)*1000)/10+"%";
    }
  ).start();

请提供一个完整的例子。异步调用的方式/位置?不清楚如何退出嵌套for循环,这一点都不重要。我将添加一些代码,让您大致了解它是如何完成的。@最后面的部分是如何不清楚的?如果不满足条件,它将退出,就像任何其他for循环一样。
  new ProgressManager(
    function() {
      //x and y from prewious iterations
      var x = this.x;
      var y= this.y;
      //Canvas related stuff
      var array = this.imageData.data;
      var width = this.imageData.width;
      var height = this.imageData.height;
      //step in this progress execution
      var step=0;
      //Iterate through x and y, starting from prewious values
      for (;y < height&&step<1000; y++) {
        //Cache computed y offset
        var ofy = y * width;
        for (;x < width&&step<1000; x++) {
          //Do some canvas operation here

          //Iterate step
          step++;
        }
      }
      this.x = x;
      this.y = y;
      this.progress+=step;
      return new ProgressEvent(null, null, variables.progress/(width*height),1);
    },
    //Data for the progress
    {x:0,y:0,progress:0,ctx:ctx,imageData:data},
    //onFinish callback
    function() {
      console.log("DONE!");
      this.ctx.putImageData(this.imageData);    
    },
    //onProgress callback
    function(event) {
      document.title=Math.round((event.progress)*1000)/10+"%";
    }
  ).start();