for循环中断的Javascript切换;冲突

时间:2013-09-16 05:39:23

标签: javascript design-patterns

我在JavaScript中使用循环嵌套切换,如:

for (var i = 0; i < checkBoxIds.length; i++) {
        if ($('#' + checkBoxIds[i]).prop('checked')) {
            var id = checkBoxIds[i];
            var assetCat = id.substring(0, 4);
            switch (id.substring(id.length - 3)) {
                case "scr":
                    if (!sscripts)
                        if (confirm("Name of scripts sub-folder (in shared) is not provided for " + assetCat + ". Press OK to Continue for others?"))
                            continue; else break; //else return
                    //Appending chrs or sets or props to scripts
                    switchAssets(sscripts, IdEnum.SCRIPTS);
                    break;
                case "shd":
                    if (!sshaders)
                        if (confirm("Name of shaders sub-folder (in shared) is not provided for " + assetCat + ". Press OK to Continue for others?"))
                            continue; else break; //else return
                    //Appending chrs or sets or props to shaders
                    switchAssets(sshaders, IdEnum.SHADERS);
                    break;
                case "sim":
                    if (!ssourceimages)
                        if (confirm("Name of sourceimages sub-folder (in shared) is not provided for " + assetCat + ". Press OK to Continue for others?"))
                            continue; else break; //else return
                    //Appending chrs or sets or props to sourceimages
                    switchAssets(ssourceimages, IdEnum.SOURCEIMAGES);
                    break;
                default:
            }
        }
    }

    //...Still doing something (else return; will never kiss this :D )
}

如果!sscripts 是假的,我问用户是否想要继续其他复选框,如果他取消,我想打破循环并执行函数中的剩余语句。好像是休息;在执行switch的确认对话框中,如何让它为for循环运行。

任何建议都将受到赞赏。

2 个答案:

答案 0 :(得分:0)

这正是labels的用途:

theloop:
  for (var i = 0; i < checkBoxIds.length; i++) {
    ...

    switch (id.substring(id.length - 3)) {

      ...
        break theloop;
        // continue theloop;

答案 1 :(得分:-1)

而不是这个

 continue; else break; //else return

试试这个

continue; else break iWantHere; //打破标签

将此标签iWantHere添加到您希望控件转到的位置

Example

 for(...)
 {
    switch('a')
    {
       case 'a': break iWantHere; // This will exit out of loop
       default:
    }
 }  

 iWantHere :
  // Rest of your code