javascript - AngularJS execute a function after the completion of another function - Stack Overflow

I execute a function that calls two functions sequentially, to run the second function should first fin

I execute a function that calls two functions sequentially, to run the second function should first finish the first one. But this does not happen, perhaps because the first function is asynchronous. I read that I need to use "promise" I tried it, in different way, but it doesn't works. So I rewrote the function as I initially wrote:

function fail() { 
    // Get the snackbar DIV
    var x = document.getElementById("snackbar")

    // Add the "show" class to DIV
    x.className = "show";

    // After 3 seconds, remove the show class from DIV
    setTimeout(function(){ x.className = x.className.replace("show", "");}, 3000);   

}

function _goback(){
    $location.url('/app/dispensers');
}    

//Check if the item is still available (if nobody bought it)
function _checkavailability(response){
    if (response.data == ""){
              console.log("Accesso non autorizzato")
    }
    $scope.infoproductbyid = response.data;
    if($scope.infoproductbyid.purchaseTime == null){
        console.log("Item disponibile");
        //if the item is available, it's possible to proceeds to checkout
        $location.url('/app/notregcheckout');
    }
    else{
        console.log("Spiacente, item non più disponibile");
      //  localStorage.clear("tokenidproduct");
        //_showSB();  
        fail();
        _goback();
    }    
}               

In the last rows, you can see that I call fail() function for first, and _goback() function for second. I want that _goback()starts when fail()finish, but fail() contains a timeout, and I think that for this reason the function is asynchronous. I don't understand how I can do

I execute a function that calls two functions sequentially, to run the second function should first finish the first one. But this does not happen, perhaps because the first function is asynchronous. I read that I need to use "promise" I tried it, in different way, but it doesn't works. So I rewrote the function as I initially wrote:

function fail() { 
    // Get the snackbar DIV
    var x = document.getElementById("snackbar")

    // Add the "show" class to DIV
    x.className = "show";

    // After 3 seconds, remove the show class from DIV
    setTimeout(function(){ x.className = x.className.replace("show", "");}, 3000);   

}

function _goback(){
    $location.url('/app/dispensers');
}    

//Check if the item is still available (if nobody bought it)
function _checkavailability(response){
    if (response.data == ""){
              console.log("Accesso non autorizzato")
    }
    $scope.infoproductbyid = response.data;
    if($scope.infoproductbyid.purchaseTime == null){
        console.log("Item disponibile");
        //if the item is available, it's possible to proceeds to checkout
        $location.url('/app/notregcheckout');
    }
    else{
        console.log("Spiacente, item non più disponibile");
      //  localStorage.clear("tokenidproduct");
        //_showSB();  
        fail();
        _goback();
    }    
}               

In the last rows, you can see that I call fail() function for first, and _goback() function for second. I want that _goback()starts when fail()finish, but fail() contains a timeout, and I think that for this reason the function is asynchronous. I don't understand how I can do

Share edited Jun 5, 2017 at 1:13 Kasiriveni 5,9711 gold badge23 silver badges32 bronze badges asked Jun 4, 2017 at 17:32 Giao BrunettaGiao Brunetta 1,5773 gold badges22 silver badges39 bronze badges 10
  • You can put _goback(); function inside of fail() function time out function like setTimeout(function() { x.className = x.className.replace("show", ""); _goback(); }, 3000); – Anupam Commented Jun 4, 2017 at 17:37
  • I already tryed in this way; ' setTimeout(function(){ x.className = x.className.replace("show", ""); _goback();}, 3000);' but in this case _goback() doesn't works – Giao Brunetta Commented Jun 4, 2017 at 17:38
  • THe second function doesn't start in this way – Giao Brunetta Commented Jun 4, 2017 at 17:39
  • Did you get any error? – Anupam Commented Jun 4, 2017 at 17:39
  • Then put another setTimeout like setTimeout(function() { x.className = x.className.replace("show", ""); setTimeout(function(){ _goback(); },0) }, 3000); – Anupam Commented Jun 4, 2017 at 17:41
 |  Show 5 more ments

1 Answer 1

Reset to default 6

Use the $timeout service to create a promise:

function fail() { 
    // Get the snackbar DIV
    var x = document.getElementById("snackbar")

    // Add the "show" class to DIV
    x.className = "show";

    // After 3 seconds, remove the show class from DIV
    var promise = $timeout(function() {
      x.className = x.className.replace("show", "");
    }, 3000);

    //RETURN promise   
    return promise;
}

Then use the .then method to wait:

fail().then(function() {
    _goback();
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744809021a4594952.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信