2013-07-11 112 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调用执行回调函数。