javascript - Loading finishing before everything has loaded from multiple functions -


i'm sure going simple, having problem when calling function needs various different function calls via ajax .net code behind , want show loader on page until has finished, not happening.

function expand(id, user) {  $('.loadingblacksml, .loadingsml').fadein(1000); checksession(); expand2(id, user); $('.loadingblacksml, .loadingsml').fadeout(1000); } 

which calls

function checksession() { return $.ajax({     type: "post",     url: "/test.aspx/checkforsession",     //data: "{}",     data: "{'idletime':'" + clickeddt + "'}",     contenttype: "application/json; charset=utf-8",     datatype: "json",     success: function (sessioncheck) {         sessionactive = json.stringify(sessioncheck.d);     } }).done(function () {     //if session comes dead, redirect restart session     if (sessionactive == "\"false\"") {         var url = "/error.aspx?refreshneeded=true&page=" + window.location.pathname;         $(location).attr('href', url);     }     //if page has gone past timeout length, try , reload     else if (sessionactive == "\"timeout\"") {         var urlto = window.location.pathname;         $(location).attr('href', urlto);     } }); } 

and

function expand2(id, user) { return $.ajax({     type: 'post',     contenttype: "application/json; charset=utf-8",     url: '/test.aspx/markexpanded',     data: "{'id':'" + id + "', 'user':'" + user + "'}",     async: false,     success: function (response) {     },     error: function ()     { console.log('there error'); }  }).done(function () { }); } 

but loading overlays disappearing before finishing doing doing? i've seen using $.when calls i'm not sure how working properly?

any advice great. thanks

try hide loading in expand2 or checksession function on finish request. this

... .done(function () {     $('.loadingblacksml, .loadingsml').fadeout(1000); }); 

so loader hidden after have finished. sure over, can set "flag". example check = 2. , do

check--; check||$('.loadingblacksml, .loadingsml').fadeout(1000); 

edited 10.04.17

example flag (check) requests

var check = 0; // count pending requests function expand(id, user) {  $('.loadingblacksml, .loadingsml').fadein(1000); count = 2; checksession(); expand2(id, user); }  function checksession() { return $.ajax({     type: "post",     url: "/test.aspx/checkforsession",     //data: "{}",     data: "{'idletime':'" + clickeddt + "'}",     contenttype: "application/json; charset=utf-8",     datatype: "json",     success: function (sessioncheck) {         sessionactive = json.stringify(sessioncheck.d);     } }).done(function () {     //if session comes dead, redirect restart session     if (sessionactive == "\"false\"") {         var url = "/error.aspx?refreshneeded=true&page=" + window.location.pathname;         $(location).attr('href', url);     }     //if page has gone past timeout length, try , reload     else if (sessionactive == "\"timeout\"") {         var urlto = window.location.pathname;         $(location).attr('href', urlto);     }     count--;     if (count === 0) { // check. requests finished         $('.loadingblacksml, .loadingsml').fadeout(1000);      } }); } function expand2(id, user) { return $.ajax({     type: 'post',     contenttype: "application/json; charset=utf-8",     url: '/test.aspx/markexpanded',     data: "{'id':'" + id + "', 'user':'" + user + "'}",     async: false,     success: function (response) {     },     error: function ()     { console.log('there error'); }  }).done(function () {     count--;     if (count === 0) {         $('.loadingblacksml, .loadingsml').fadeout(1000);      } }); } 

of course, can move

    count--;     if (count === 0) {         $('.loadingblacksml, .loadingsml').fadeout(1000);      } 

to separate function


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -