如何(break; while)和(continue; loop)

时间:2017-11-10 20:56:34

标签: javascript loops for-loop break

HHi,我有一个小问题,很难解释,但我在循环中有一段时间。

我的插件的上下文要求重置循环。 (继续) 但是,我还需要做一段时间(休息)。

我怎样才能打破并打电话给继续;为了循环。

这里是完整的上下文,我知道很难从自定义上下文中读取代码 但这里是我的功能范围和尝试目标。

查看此行 console.log('=> how break and continue') 感谢您的帮助

    // STEP3: BUILD TEXT CHILD
var re = /\w+.|\W/g; // \w+.\s*|\W+ // if wordWrap only
var l = -1; // START AT LINE -1 (line++ at start loop)
var lineX = 0;
var lineY = 0;
var lineHeight = 0; // at end maxheight auto increase
var newLine=true; // jmp line and creat
var dataLine = [];
var maxLineX = this._style.global.wordWrap && this._style.global.wordWrapWidth || false;
var pixiBox = false;;
for(var d=0, newLine=true; d<dataTxt.length; d++){ // loop all dataTxt

    var data = dataTxt[d];
    var dataL = dataLine[l];
    if(newLine){ // INITIALISE NEW LINE AND RESET DATA
        lineY+=lineHeight; 
        lineX = 0; 
        lineHeight = 0; 
        newLine = false; 
        l++;
        dataLine[l] = {width:0, height:0, txtID:[], line:l }; // initialise a new this._dataLines [line]
        var dataL = dataLine[l];
    };

    // create type of elements
    if(!pixiBox){ // if empty pixi box, create new data pixi
        if(data.type==='txt'){ 
            pixiBox = new PIXI.Text(data.value, data.style);
        }else if(data.type==='icon'){ // if icons was not registered, registe to pixiBox;
            pixiBox = new PIXI.Sprite(pixiMS._iconsID[data.value]);
        }else if(data.type==='jmp'){
            pixiBox =  new PIXI.Graphics();
            pixiBox.drawRect(0, 0, 0, dataL.height+data.value);
            newLine = true;
        };
    };



    // check if its ou limit ?
    if(maxLineX && (lineX + pixiBox.width) > maxLineX){
        if(d>5000){confirm('ERROR EXCEED LIMIT WORDWRAP < WORD LENGTH, OR USE BREAK WORDS'); break; }; // protection freeze engine (win) if wrong wordWrap size
        if(data.type==='icon'){newLine = true; d--; continue; }; // reset to a newLine, but keep icons registered 

        // its a text need Split to a new line;
        var letterWidth = pixiBox.width / data.value.length; // calculate width of all letter
        var tmpW = 0; // Temps Width
        while ( (match = re.exec(data.value) ) !== null) {
            var mL = match[0].length; // match txt length 
            if(lineX+tmpW+(mL*letterWidth) > maxLineX){ // if this match exeed , txt befor index become this valur and add extra data after
                if(match.index===0){ // if match index 0 , (this data continue) reset new line 
                    console.log('=> how break and continue');newLine = true; d--; break; continue;
                }else{ // current txt become txt befor , and push new data after
                    data.value = match.input.slice(0, match.index); // text befor the match (if 0 its ok, just empty txt)
                    var after1 = this._newData('jmp', 0, "wordWrapBreak", false);
                    var after2 = this._newData('txt', match.input.slice(match.index), data.tag, data.style);
                    this._dataTxt.splice(d+1, 0, after1,after2); // reconfigu the loop length
                    pixiBox.text =  data.value; // redefine pixi
                    break;
                }

            };
            tmpW+=lineX;
        };
    };


    data.xPos = lineX; 
    data.yPos = lineY; 
    data.line = l;

    // INCREASE TXT POSITION
    lineX += pixiBox.width; // x pos for next pixi element
    dataL.width = lineX; // line width ++ valueOf()
    lineHeight = pixiBox.height>lineHeight&&pixiBox.height||lineHeight;

    dataL.height = lineHeight;
    dataL.txtID.push(d);
    this._childTexts.push(pixiBox); // store child array
    this.addChild(pixiBox);
    pixiBox = false;
};
return this;

编辑解决:

        if(maxLineX && (lineX + pixiBox.width) > maxLineX){
        if(d>5000){confirm('ERROR EXCEED LIMIT WORDWRAP < WORD LENGTH, OR USE BREAK WORDS'); break; }; // protection freeze engine (win) if wrong wordWrap size
        if(data.type==='icon'){newLine = true; d--; continue; }; // reset to a newLine, but keep icons registered 

        // its a text need Split to a new line;
        var letterWidth = pixiBox.width / data.value.length; // calculate width of all letter
        var tmpW = 0; // Temps Width
        var skip = false;
        while ( (match = re.exec(data.value) ) !== null) {
            var mL = match[0].length; // match txt length 
            if(lineX+tmpW+(mL*letterWidth) > maxLineX){ // if this match exeed , txt befor index become this valur and add extra data after
                if(match.index===0){ // if match index 0 , (this data continue) reset new line 
                    console.log('=> how break and continue');newLine = true; d--; skip=true; break;
                }else{ // current txt become txt befor , and push new data after
                    data.value = match.input.slice(0, match.index); // text befor the match (if 0 its ok, just empty txt)
                    var after1 = this._newData('jmp', 0, "wordWrapBreak", false);
                    var after2 = this._newData('txt', match.input.slice(match.index), data.tag, data.style);
                    this._dataTxt.splice(d+1, 0, after1,after2); // reconfigu the loop length
                    pixiBox.text =  data.value; // redefine pixi
                    break;
                }

            };
            if(skip){continue;}
            tmpW+=lineX;
        };
    };

2 个答案:

答案 0 :(得分:2)

您需要的是标签。

forloop:
    for(){
        whileloop: 
            while(){
                break whileloop;
            }
    }

这将退出while循环,但仍保留在for循环中。在此处阅读更多内容:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

答案 1 :(得分:2)

这是丑陋的IMO,但你可以这样做:

outer: for(var d=0, newLine=true; d<dataTxt.length; d++){ // loop all dataTxt
       //...
           while ( (match = re.exec(data.value) ) !== null) {
           //...
               if(match.index===0){
                   continue outer;
               }
           }
       }

我可能会做的是:

for(var d=0, newLine=true; d<dataTxt.length; d++){ // loop all dataTxt
     //...
     var skip = false;
     while ( (match = re.exec(data.value) ) !== null) {
          if(match.index===0){
              skip = true;
              break;
          }
     }
     if (skip) {
         continue;
     }
     //...
}