Ajax请求和回调更新正在引发在同一时间 [英] Ajax request and callback update are triggering at the same time
问题描述
我有一些提交被以下面的方式定义的按钮:
I have some submit buttons that are defined in the following manner:
<input id="add_200,231" type="submit" onclick="updatePage(401,27371,200,231)" value="Add"/>
相应的引用的JavaScript函数的onclick看起来是这样的:
The corresponding javascript function referenced in "onclick" looks like this :
function updatePage(slac,sci,tlac,tci) {
var url = '/neighbouring?.state=add_cell&source=' + slac + ',' + sci +'&target='+ tlac + ',' + tci + '&dummy='+(new Date()).getTime();
new Ajax.Request(url,{
onComplete: triggerContentUpdate(slac,sci)
});
}
function triggerContentUpdate(slac,sci) {
var updates = document.getElementsByClassName('update_on_add');
for (var i = 0; i < updates.length; i++) {
var uid = updates[i].id;
var url = '/neighbouring?.state=update_template&divid=' + uid + '&source='+slac + ',' +sci + '&dummy='+(new Date()).getTime();
var uAjax = new Ajax.Updater(uid,url,{ method: 'get'});
}
}
这需要更新被标记为update_on_add类的标签。
The elements that need to be updated are tagged with "update_on_add" class tags.
当使用萤火虫,我可以看到,所述的Ajax.Request中的updatePage()以及Ajax.Update在triggerContentUpdate()函数调用同时。我本来期望triggerContentUpdate()仅被调用后的Ajax.Request完成。
When using Firebug, i can see that the Ajax.Request in updatePage() as well as the Ajax.Update in the triggerContentUpdate() functions are called at the same time. I would have expected triggerContentUpdate() to only be called after Ajax.Request has completed.
我失去了一些东西呢?
推荐答案
您行
onComplete: triggerContentUpdate(slac,sci)
应
onComplete: function() { triggerContentUpdate(slac,sci); }
您会立即调用该函数,并指派其返回值的的onComplete成员。
You're calling the function immediately and assigning its return value to the onComplete member.
这篇关于Ajax请求和回调更新正在引发在同一时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!