我的设置
function ajaxLoadSth(value, attribute) {
$.post( url, value, function(response) {
// interpreting the response
var foobar = $('#foo') + $('.bar').find('.sth');
// print interpretation into DOM
container.append(foobar);
// do some funky stuff
doSomeFooWithDom();
checkSomeBarHere();
updateSomeValuesThere();
// now call the function again with different values
// THIS IS WHAT MY QUESTION REFERS TO **************
if ( true ) {
ajaxLoadSth(newValue, newAttribute);
}
});
}
我的问题
函数不得在//做一些时髦的事情之前调用自身 - 部分,一些 DOM 操作,已完成执行 - 但它确实强>.
我的问题
- 这是为什么呢?是因为那些“时髦的东西”位于外部函数中吗?这对于同步执行来说重要吗?
- 如何让“自身调用”等到上面的内容完成?
我已经考虑过通过设置全局变量和触发处理程序来告诉函数它们已准备就绪。每次触发处理程序时,函数都会检查所有变量是否设置正确,然后执行函数调用。那又怎么样?
非常感谢您提前抽出时间!
最佳答案
尝试回调 例如。
doSomeFunkyStuff(callback){
//DO SOME FUNKY
callback();
}
function ajaxLoadSth(value, attribute) {
$.post( url, value, function(response) {
// interpreting the response
var foobar = $('#foo') + $('.bar').find('.sth');
// print interpretation into DOM
container.append(foobar);
// do some funky stuff
doSomeFunkyStuff(function(){
ajaxLoadSth(newValue, newAttribute);
}
});
}
关于javascript - 如何在使用 AJAX 时控制 JavaScript 函数的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14987415/