I have following code with multiple 'if' statements.
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 goes on till 7, and most of the code inside 'if' is same except for few statements, so I guess there is definitely a scope for optimizing it, but I do not exactly know how.
How can I do this?
UPDATE: Removed incorrect description of problem statement.
$scope.counters = []and then index them by number?ifs and not disjoint, you should rather useif...else if...elseand notif.. if