2013-07-11 115 views
0

我是JavaScript編程新手。我無法找到有效的答案。等待機箱的異步方法完成javascript

的問題是,當它被包裹在setTimeout調用像這樣我的功能只適用:

var sPageIdentifier = 'ReportViewer'; 
UserPreferencesManager.Initialize(sPageIdentifier); 
setTimeout(function() { 
var strUserPrefs = UserPreferencesManager.GetPreferences(); 
    console.log(strUserPrefs); 
    initLayout(strUserPrefs); 
}, 1000); 

function initLayout(strUserPrefs) { 
    //do stuff using strUserPrefs 
} 

如果我註釋掉的setTimeout功能,InitLayout在(strUserPrefs),因爲strUserPrefs爲空失敗。 任何幫助將不勝感激!

這裏是UserPreferencesManager.js代碼:

var UserPreferencesManager = function() { 
    var strPrefsID = null; 
    var strPrefsString = null; 

    return { 

    Initialize: function (strPrefsIDIn) { 
     strPrefsID = strPrefsIDIn; 
     strPrefsString = this.GetPreferences(); 
    }, 

    GetPreferences: function() { 
     if (!strPrefsID) { 
     alert("Validation Failed: the UserPreferencesManager must be initialized prior to usage."); 
     return null; 
     } 
     if (!strPrefsString) { 
     this.LoadPreferences(); 
     return strPrefsString; 
     } 
     return strPrefsString; 
    }, 
    LoadPreferences: function() { 
     if (!strPrefsID) { 
     alert("Validation Failed: the UserPreferencesManager must be initialized prior to usage."); 
     return null; 
     }  
     myasyncfunctioncall({ 
     parameters: ["USR_PersonId", "abc", 'GET'] 
     script_name: 'MAINTAIN_USER_PREFS', 
     onexception: function (exception, xhr, options) { 
      alert('Error: ' + xhr.statusText + +exception); 
      console.log(exception); 
     }, 
     onsuccess: function (data, xhr, options) { 
      if (data == "User ID is zero") { 
      alert('MP_MAINTAIN_USER_PREFS: must be > 0.0'); 
      strPrefsString = data; 
      } 
      else { 
      strPrefsString = data; 
      } 
     } 
     }); 
    },// end of LoadPreferences 

    WritePreferences: function (strPrefsIn, strPrefsID) { 
     if (strPrefsID && typeof strPrefsID === "string") { 
     if (strPrefsIn != null) { 

      myasyncfunctioncall({ 
      parameters: ["USR_PersonId", strPrefsID, strPrefsIn , 'SET'] 
      script_name: 'MAINTAIN_USER_PREFS', 
      onexception: function (exception, xhr, options) { 
       alert('Error: ' + xhr.statusText + +exception); 
       console.log(exception); 
      }, 
      onsuccess: function (data, xhr, options) { 
       if (data == "transaction-ok") { 
       UserPreferencesManager.LoadPreferences(); 
       } else if (data == "User ID is zero") { 
       alert('MP_MAINTAIN_USER_PREFS: must be > 0.0'); 
       } 
      } 
      }); 
     } else { 
      alert("Error: Preferences object must be initialized prior to writing preferences"); 
     } 
     } else { 
     alert('Error: The preference ID can\'t be null and must to be of type string'); 
     return; 
     } 
    }// end of WritePreferences 
    };// end of return API 

}(); // end of UserPreferencesManager 
+0

瞭解延遲對象/承諾:http://learn.jquery.com/code-organization/deferreds/。 –

+0

謝謝,會做。 –

回答

1

感謝您的提示,Balachandra! 這裏是我做什麼,添加了兩個參數LoadPreferences方法 - 回調strPrefsID,這樣我就可以成功中調用fnCallback功能,並將它傳遞阿賈克斯數據:

LoadPreferences: function (fnCallback, strPrefsID) { 
    if (!strPrefsID) { 
     alert("Validation Failed: the BhsUserPreferencesManager must be initialized prior to usage."); 
     return null; 
    } 
    if (strPrefsString) { 
     // strPrefsString is not null, so return it 
     callback(strPrefsString); 
    } else { 
     myasyncfunctioncall({ 
      parameters: ["USR_PersonId", "abc", 'GET'] 
      script_name: 'MAINTAIN_USER_PREFS', 
      onexception: function (exception, xhr, options) { 
       alert('Error: ' + xhr.statusText + +exception); 
       console.log(exception); 
      }, 
      onsuccess: function (data, xhr, options) { 
       if (data == "User ID is zero") { 
        alert('MAINTAIN_USER_PREFS: must be > 0.0'); 
        strPrefsString = data; 
       } else if (data.substring(0, 5) === "ERROR") { 
        alert(data); 
       } else { 
        fnCallback(data); 
       } 
      } 
     }); 
    } 
}// end of LoadPreferences 

,這裏是如何,現在我可以打電話InitLayout在:

BhsUserPreferencesManager.LoadPreferences(initLayout, sPageIdentifier); 
0

正在發送一個異步請求作用似乎這樣myasyncfunctioncall。如果此異步請求的響應已到達,則需要添加一些變量以設置該變量,然後,在設置該變量時,您可以繼續執行例程。

每當在javascript上進行異步調用時,程序就會繼續,就好像它已經完成一樣。你必須手動添加檢查,看看它是否已完成。

0

UserPreferencesManager.GetPreferences()正在進行異步AJAX調用以獲取用戶首選項。因此,在這種情況下,Javascript線程將在當前線程上下文中繼續執行,並執行initLayout(strUserPrefs)。但在此狀態下,GetPreferences()調用仍未完成,並且strUserPrefs爲空。

SetTimeout是解決這個問題的技巧之一,你做了。但是您也可以通過這樣的方式設計API,即允許每個異步AJAX調用執行回調函數。