2017-04-07 1 views
0

이것이 정말 간단 할 것이라고 확신하지만 ajax를 통해 다양한 다른 함수 호출을 수행해야하는 함수를 호출 할 때 문제가 발생합니다. 뒤에서 나는 모든 것이 끝날 때까지 페이지 위에 로더를 보여주고 싶지만 일어나지는 않습니다.다기능에서 모든 것이로드되기 전에 마무리 작업로드

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 back as dead, redirect to 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 and reload it 
    else if (sessionActive == "\"timeout\"") { 
     var urlTO = window.location.pathname; 
     $(location).attr('href', urlTO); 
    } 
}); 
} 

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 is some error'); } 

}).done(function() { 
}); 
} 

그러나로드 오버레이가이 일을 일을 그 전에 사라지고 마무리되어 있습니다를 호출

function expand(ID, user) { 

$('.loadingBlackSml, .loadingSml').fadeIn(1000); 
checkSession(); 
expand2(ID, user); 
$('.loadingBlackSml, .loadingSml').fadeOut(1000); 
} 

? 나는 내 전화에 $ .when을 사용하는 것에 대해 뭔가를 보았지만 제대로 작동하는 방법을 모르겠다.

어떤 조언은 좋은 것입니다. 감사합니다

+0

FYI의 "코드 숨김"은 백엔드라고합니다. 따라서 .NET 백엔드가 있습니다. –

답변

0

마무리 요청시 expand2 또는 checkSession 함수에로드를 숨기려고합니다. 이와 같이

모든 작업이 완료되면 로더가 숨겨집니다. 모든 것이 끝났 으면 "플래그"를 설정할 수 있습니다. 예를 점검 들어 = 2 그리고는

check--; check||$('.loadingBlackSml, .loadingSml').fadeOut(1000); 

물론
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 back as dead, redirect to 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 and reload it 
    else if (sessionActive == "\"timeout\"") { 
     var urlTO = window.location.pathname; 
     $(location).attr('href', urlTO); 
    } 
    count--; 
    if (count === 0) { // check. are all 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 is some error'); } 

}).done(function() { 
    count--; 
    if (count === 0) { 
     $('.loadingBlackSml, .loadingSml').fadeOut(1000); 
    } 
}); 
} 

, 당신은

count--; 
    if (count === 0) { 
     $('.loadingBlackSml, .loadingSml').fadeOut(1000); 
    } 
이동할 수 있습니다 요청 플래그 (체크)와 10.04.17

예를 편집 할

~ 기능 분리

+0

거의 없지만 checkSession() 후에 expand2()가 끝날 것이라고 가정 할 수 없습니다 ... 두 done() 메서드에서 모두 콜백을 호출해야합니다. 둘 다 완료되면 fadeOut()을 호출 할 수 있습니다 –

+0

예, ive는 이미 그것을 알아 차렸다. 게시물이 업데이트되었습니다 –

+0

감사합니다. expand2는 항상 페이지를 호출 한 다른 기능의 마지막 기능이 아닙니다. 귀하의 게시물의 두 번째 부분에있는 수표 내용에 대해 약간 혼란 스럽습니까? – slowlygettingthere