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
- 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
1 Answer
Reset to default 6Use 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条)