gpt4 book ai didi

javascript - 如何在使用 AJAX 时控制 JavaScript 函数的顺序

转载 作者:行者123 更新时间:2023-11-28 09:20:54 24 4
gpt4 key购买 nike

我的设置

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com