我在找这样的东西
function someFunc() {
callAjaxfunc(); //may have multiple ajax calls in this function
someWait(); // some code which waits until async calls complete
console.log('Pass2');
}
function callAjaxfunc() {
//All ajax calls called here
console.log('Pass1');
}
What I have tried?
1 Jquery.when()
tried using it..it works fine. But not the way I want. $.when
will wait but the code next to $.when()
runs with out waiting. The code inside do callback
only runs after ajax calls
2. setTimeOut() with a global flag
我很有信心这会奏效.我试着跟着.
GlobalFlag = false;
function someFunc()
callAjaxfunc(); //may have multiple ajax calls in this function
setTimeOut(waitFunc, 100); // some which waits until async calls complete
console.log('Pass2');
}
function callAjaxfunc() {
//All ajax calls called here
onAjaxSuccess: function() {
GlobalFlag = true;
};
console.log('Pass1');
}
function waitFunc() {
if (!GlobalFlag) {
setTimeOut(waitFunc, 100);
}
}
Still not able to get wanted result. Am I doing something wrong here? This is not the way?
我想要的结果应该是这样的
Pass1
Pass2
由于需要AJAX调用,无法进行任何小提琴操作
EDIT: As many were suggesting callbacks..i know about them..but still the code next to somewait()
will get executed...I want browser to completely stop executing code next to somewait()
until the ajax call..Also it may be a bad practice but worth to know and try if possible...