次のような関数があります。
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 呼び出しを介して成功を返し (それが行う)、 という新しいsaveCardInfo
オブジェクトを作成することcardInfo
です。私が知る限り、それは起こりcardInfo.cc.cc_type
ますが、オブジェクトが作成される前に行のチェックとそれ以降のすべてが行われます。Firebug コンソールのスクリーンショットを添付したので、物事が起こっている順序は明らかです。
私は何を間違っていますか?関数をさらに続行する前にpoGetPaymentOption
、完全に作成されていることを確認する必要があります。cardInfo
showCreditCard