簡體   English   中英

除非指定,否則不要繼續Javascript for循環

[英]Do not continue Javascript for-loop until specified

我需要暫停for循環,直到指定后再繼續。 對於我要遍歷的數組中的每個項目,我運行一些代碼以在單獨的設備上運行操作,並且我需要等到該操作完成后才能循環到數組中的下一個項目。

幸運的是,該代碼/操作是游標,並且具有after:部分。

但是,它一直在立即運行整個for循環,這需要防止。 有什么方法可以防止循環繼續進行直到指定? 還是其他類型的循環或我應該使用的東西?

我的第一個(較差的)想法是在連續運行的for循環中創建一個while循環,直到after:部分游標將boolean設置為true 這只是鎖定了瀏覽器:(正如我擔心的那樣。

我能做什么? 我對javascript很陌生。 我一直在享受我當前的項目。

這是while-loop嘗試。 我知道它立即運行了整個循環,因為dataCounter立即從1變為3 (當前數組中的兩項):

if(years.length>0){
  var dataCounter = 1;
  var continueLoop;
  for(var i=0;i<years.length;i++){
    continueLoop = false;
    baja.Ord.make(historyName+"?period=timeRange;start="+years[i][1].encodeToString()+";end="+years[i][2].encodeToString()+"|bql:select timestamp, sum|bql:historyFunc:HistoryRollup.rollup(history:RollupInterval 'hourly')").get(
        {
          ok: function (result) {
          // Iterate through all of the Columns

          baja.iterate(result.getColumns(), function (c) {
            baja.outln("Column display name: " + c.getDisplayName());
          });
        },
        cursor: {
          before: function () {
          baja.outln("Called just before iterating through the Cursor");
          counter=0;
          data[dataCounter] = [];
          baja.outln("just made data["+dataCounter+"]");
        },
        after: function () {
          baja.outln("Called just after iterating through the Cursor");
          continueLoop = true;
        },
        each: function () {

          if(counter>=data[0].length) {
            var dateA = data[dataCounter][counter-1][0];
            dateA += 3600000;
          }
          else {
            var dateA = data[0][counter][0];
          }

          var value=this.get("sum").encodeToString();
          var valueNumber=Number(value);

          data[dataCounter][counter] = [dateA,valueNumber];
          counter++;
        },
        limit: 744, // Specify optional limit on the number of records (defaults to 10)2147483647
        offset: 0 // Specify optional record offset (defaults to 0)
        }
        })
        while(continueLoop = false){
          var test = 1;
          baja.outln("halp");
        }
    dataCounter++;
  }
}

不要使用for循環在每個元素上循環。 接下來,您需要after:記住剛完成數組的哪個元素,然后移至下一個元素。

像這樣的東西:

var myArray = [1, 2, 3, 4]

function handleElem(index) {
    module.sendCommand({
        ..., // whatever the options are for your module
        after: function() {
            if(index+1 == myArray.length) {
                return false; // no more elem in the array
            } else {
                handleElem(index+1)}  // the after section
            }
    });
}

handleElem(0);

我假設您調用了帶有某些選項的函數(就像您對$.ajax()所做的那樣),而after()節是在過程結束時調用的函數(如$.ajax() success() $.ajax()

如果您調用的“模塊”未在after()回調中正確結束,則可以使用setTimeout()在下一個元素上延遲啟動進程

編輯:用您的真實代碼將是這樣的:

function handleElem(index) {
    baja.Ord.make("...start="+years[index][1].encodeToString()+ "...").get(
    {
        ok: ...
        after: function() {
            if(index+1 == years.length) {
                return false; // no more elem in the array
            } else {
                handleElem(index+1)}  // the after section
            }
        }
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM