I found there are two methods to code an if-else statement with a timeout function. Two of these scripts work fine, but which one of them is more preferable and why?
Method One
app.controller("filter", function($scope, $timeout) {
$scope.expand = function() {
$timeout(function() {
if ($scope.expandCollapse === "expand-up")
$scope.expandCollapse = "expand-down";
else
$scope.expandCollapse = "expand-up";
}, 1500);
};
});
Method Two
app.controller("filter", function($scope, $timeout) {
$scope.expand = function() {
if ($scope.expandCollapse === "expand-up")
$timeout(function() {
$scope.expandCollapse = "expand-down";
}, 1500);
else
$timeout(function() {
$scope.expandCollapse = "expand-up";
}, 1500);
};
});
$scope.expandcan be called multiple times in a short time these two snippets will behave differently. The question is: What behaviour do you want? \$\endgroup\$