這是場景:
btnplus函式 - 將一些新資料插入資料庫
它呼叫get_atitles- 這也是一個用新資料集填充 div 的 ajax 呼叫
當 div 被填充時 -go_find應該在新集合中找到一個特定元素。這不是一個ajax
問題 -在完成go_find之前開始,get_atitles因此無法找到所需的資料
我試圖放置async await在各個地方 - 沒有成功
go_find那么完成后如何呼叫get_atitles呢?
$(btnplus).on('click', function(){
...
$.post('pro.php', {fn: 'btn_plus_fi', args: [path]}, async function(data){
// some new data is added to database
await get_atitles(); // load the new set of data into a div
go_find(str); // find a specific data inside a new set
});
});
function get_atitles(){
...
$.post('pro.php', {fn: 'get_atitles', args: [par]}, function(data){
atitles.html(data);
});
}
function go_find(str){
// find something inside `atitles`
}
uj5u.com熱心網友回復:
Re: 那么如何在 get_atitles 完成后呼叫 go_find 呢?
只需將go_find函式呼叫放在get_atitles結果處理程式中。
$(btnplus).on('click', function() {
$.post('pro.php', {
fn: 'btn_plus_fi',
args: [path]
}, async function(data) {
// some new data is added to database
await get_atitles(); // load the new set of data into a div
});
});
function get_atitles() {
$.post('pro.php', {
fn: 'get_atitles',
args: [par]
}, function(data) {
atitles.html(data);
go_find(str); // find a specific data inside a new set
});
}
function go_find(str) {
// find something inside `atitles`
}
選擇
$(btnplus).on('click', function () {
$.post('pro.php', { fn: 'btn_plus_fi', args: [path] }, async function (data) {
// some new data is added to database
$.post('pro.php', { fn: 'get_atitles', args: [par] }, function (data) {
atitles.html(data);
go_find(str); // find a specific data inside a new set
});
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/486935.html
標籤:javascript jQuery 阿贾克斯 异步等待
上一篇:用雄辯的方式查詢里面
下一篇:Unity-UI位置
