Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 优化角度域中的if语句_Javascript_Angularjs - Fatal编程技术网

Javascript 优化角度域中的if语句

Javascript 优化角度域中的if语句,javascript,angularjs,Javascript,Angularjs,我有以下带有多个“if”语句的代码 if($scope.level===1){ $scope.leftWordList=true $scope.previewViewRight=true $scope.counter1=5 $timeout($scope.startFilling, 5000) $scope.onTimeout = function(){ $scope.counter1--; mytimeout = $time

我有以下带有多个“if”语句的代码

if($scope.level===1){

    $scope.leftWordList=true
    $scope.previewViewRight=true

    $scope.counter1=5
    $timeout($scope.startFilling, 5000)
    $scope.onTimeout = function(){

      $scope.counter1--;
      mytimeout = $timeout($scope.onTimeout,1000);

      if($scope.counter1==0){
        $timeout.cancel(mytimeout);
        $scope.counter=0
      }
    }
    var mytimeout = $timeout($scope.onTimeout,1000); 

  }

  if($scope.level===2){

    console.log("Level 2")

    $scope.leftWordList=true
    $scope.previewViewRight=true

    $scope.counter2=5
    $timeout($scope.startFilling, 5000)
    $scope.onTimeout = function(){

      $scope.counter2--;
      mytimeout = $timeout($scope.onTimeout,1000);

      if($scope.counter2==0){
        $timeout.cancel(mytimeout);
      }
    }
    var mytimeout = $timeout($scope.onTimeout,1000); 
  }
  ....  
$scope.level一直持续到7,而“if”中的大部分代码除了少数语句外都是相同的,所以我想肯定有一个优化它的范围,但我不知道如何进行优化

我该怎么做

更新:
删除了对问题陈述的错误描述

当你发现自己有一长串这样相互排斥的分支时,问题就变成了:这些分支是根本不同的,还是有共同点可以考虑

如果它们有本质上的不同,就会立即想到
开关
或函数调度表

但是在你的例子中,它看起来是一个很多好像它们只是一个不同计数器的相同逻辑。如果是这样,请删除单个计数器属性(
counter1
counter2
,等等),并将其替换为可以索引到的计数器数组

然后获取级别(因为如果超时,您希望获得一致的值,而不是在超时发生之前处理已更改的值),并在整个过程中使用该级别,请参见
***
行:

var level = $scope.level;                      // ***
console.log("Level " + level)                  // ***

$scope.leftWordList = true
$scope.previewViewRight = true

$scope.counters[level] = 5                     // ***
$timeout($scope.startFilling, 5000)
$scope.onTimeout = function() {

    $scope.counters[level]--;                  // ***
    mytimeout = $timeout($scope.onTimeout, 1000);

    if ($scope.counter[level] == 0) {          // ***
        $timeout.cancel(mytimeout);
    }
}
var mytimeout = $timeout($scope.onTimeout, 1000);

请注意,这假设所有这些代码都在一个函数中,因此函数的其他调用不会共享
mytimeout
级别

根据给定的代码,如果更改一点,可以将其设为通用:

  • 您可以有一个
    计数器
    对象,然后根据级别更新必要的标志
  • 如果您有任何适用于most的通用逻辑,请将其添加到
    If
    s之前,然后在必要时重置
    If
  • 如果计数器已达到
    0
    ,则不应启动超时,然后将其清除。只需在初始化之前检查条件
  • 函数
    onTimeout
    在两个
    if
    s中都有类似的签名。试着把它变成一个函数,而不是在所有
    if
    s中复制它
示例代码:
$scope.counter={
计数器1:null,
计数器2:null,
}
.
.
.
//if块
$scope.leftWordList=true;
$scope.previewViewRight=true;
$scope.counter[“counter”+$scope.level]=5
$timeout($scope.startFilling,5000)
var mytimeout=$timeout($scope.onTimeout.bind(this,$scope.counter,$scope.level),1000);
.
.
.
//泛型函数
$scope.onTimeout=函数(计数器,级别){
如果(--$scope.counter[“counter”+level]==0)
mytimeout=$timeout($scope.onTimeout,1000);
其他的
$scope.counter=0
}

注意:我假设您正在其他地方设置
$scope.counter
。因此,我将其重置为else状态。

您可以构建一个有助于您的对象

该对象将包含您对不同级别的事物的所有防御,其余的可以放在“开关盒”中

大概是这样的:

lvlObjects = [];
lvlObject = {};
lvlObject.level = 1;
lvlObject.leftWordList = true;
lvlObject.previewViewRight = true;
....
lvlObjects.push(lvlObject);
//same for more levels.
//then call to a function that fill the scope with current values:
function updateLevel(currentLevel) {
    $scope.level = lvlObjects[currentLevel-1].level; //if needed
    $scope.leftWordList = lvlObjects[currentLevel-1].leftWordList;
    ....
}

“本质上,$scope.leftWordList=true&$scope.previewViewRight=true将在级别1和3中重复,而,$scope.leftWordList=true&$scope.previewViewRight=true将在级别2和4中重复。”嗯……这些真的是相同的吗?我不知道,但你不能只做
$scope.counters=[]
然后按数字索引?旁注:我强烈建议使用分号。如果您想依靠自动分号插入,请这样做;如果不是(我的建议),请确保将分号放在它们所属的位置。注意:如果所有
if
s且不相交,则应使用
if…else if…else
而不是
if。。如果
@Crowder:我更新了问题,说明问题时出错了。实际上它现在看起来很简单。我不得不使用不同的计数器,因为当我使用单个计数器变量时,不知何故计数器很快,结果变成了负数。这很奇怪。这个解决方案有效,我只做了一些修改就实现了它!谢谢