凌乱的顺序我有一个功能,看起来像这样:操作问题
function showCreditCard(idx, data) {
if(typeof cardInfo == 'undefined' && parseInt($("#cc-dropdown-" + idx + " option:selected").val(),10) > -1) {
// actual selection made, but not default, so cardInfo hasn't been set. Need to run ajax call to get credit card;
console.log("show dropdown");
console.log("there are saved cards and a selection was made");
poGetPaymentOption('credit card', parseInt($("#cc-dropdown-" + idx + " option:selected").val(),10), idx);
// this is the default card; display dropdown with default selected and static fields
console.log("supposedly after poGetPayment");
console.dir(cardInfo);
// why is this stuff running before poGetPaymentOtion finishes and returns a cardInfo object?
if(cardInfo.cc.cc_type == 'VI') { $('#cc_visaBig-'+idx).attr('class', 'cc_visaBig'); }
$('#cc-static-wrap-'+idx).show();
updateButtonState();
}
}
正如你可以通过评论看到,poGetPaymentOption
调用后线运行之前的功能实际上是完整的。我已经通过poGetPaymentOption
函数中的日志验证了这一点(下文)。
function poGetPaymentOption(type, key, bIdx) {
if(type == 'credit card') {
console.log("signed in, credit card");
$.post('/site/ajax/customers/getSingleCreditCard',
{ 'key': key },
function(data) {
if(data.success == 1) {
console.log("poPayment success");
if(typeof cardInfo == 'undefined') {
cardInfo = new saveCardInfo(data);
}
} else {
console.log("poPayment no success");
}
}, 'json');
}
}
我会希望发生的是呼叫从showCreditCard
到poGetPaymentOption
通过AJAX调用返回成功(其它),然后创建一个名为cardInfo
新saveCardInfo
对象。据我所知,它确实发生,但是检查cardInfo.cc.cc_type
及更高版本的行都是在创建对象之前发生的。我附上了我的Firebug控制台的屏幕截图,因此很明显,订单事情正在发生。
我在做什么错?我需要确保poGetPaymentOption
已完全完成,并且在继续执行showCreditCard
函数之前已创建了cardInfo
。
使用$就不是$。发布并将async设置为false。 – Cristy 2013-03-20 22:52:45
当poGetPaymentOption()结束时,您需要执行第一个方法的其余部分。你需要一个回调函数。 – 2013-03-20 22:54:06
@Cristy:不,尽管可能,但这不可取。 – Bergi 2013-03-20 22:54:15