我是来自 Java 的 Javascript 新手,想编写一个像 Java 方法一样的函数。问题是调用代码不会等待被调用函数完成,而是继续发生错误。我希望你明白我在说什么。也许你可以帮助我
if (Titanium.Facebook.loggedIn) {
// User is not logged in with his ACS Account but with its Facebook Account
// now we can check if he has an ACS Account with the same eMail Address
Titanium.API.info('User seems to be logged in to Facebook');
gFacebookAccount = gFacebookController.getLoggedInFacebookAccount();
Titanium.API.info('Logged In Facebook Account is : '+ gFacebookAccount); }
被调用的 getLoggedInFacebookAccount 看起来像这样。我试图通过超时循环延迟该方法不起作用。
exports.getLoggedInFacebookAccount = function()
{
Titanium.API.info('Started getLoggedInFacebookAccount');
var lCallBackFinished = false;
var lActualUser = null;
Titanium.Facebook.requestWithGraphPath('me', {}, 'GET', handleGraphRequestCallback);
waitForQueryCallback();
function waitForQueryCallback() {
if (!lCallBackFinished) {
Titanium.API.info('Wait for Query Callback Loop');
setTimeout(waitForQueryCallback, 100);
//
}
else
{
return lActualUser;
}
}
function handleGraphRequestCallback(pGraphCallback) {
// damn callbacks - now here the user is logged in
// and the Graph Request is (sucessfully or not) finished
Titanium.API.info('Start handleGraphRequest-Callback');
if (pGraphCallback.success) {
Titanium.API.info('Graph Request Callback Sucessful');
lActualUser = pGraphCallback.result;
} else {
Titanium.API.info('Graph Request Callback Not Sucessful');
}
lCallBackFinished = true;
}
}
控制台输出是:
[INFO][TiAPI ( 301)] User seems to be logged in to Facebook
[INFO][TiAPI ( 301)] Started getLoggedInFacebookAccount
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][TiAPI ( 301)] Logged In Facebook Account is : undefined
[INFO][TiAPI ( 301)] Function called
[ERROR][TiJSError( 301)] (main) [214,2413] ----- Titanium Javascript Runtime Error -----
[ERROR][TiJSError( 301)] (main) [0,2413] - In undefined:1,1
[ERROR][TiJSError( 301)] (main) [0,2413] - Message: Uncaught SyntaxError: Unexpected token u
[ERROR][TiJSError( 301)] (main) [1,2414] - Source: undefined
[ERROR][V8Exception( 301)] Exception occurred at undefined:1: Uncaught SyntaxError: Unexpected token u
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][TiRootActivity( 301)] (main) [0,0] checkpoint, on root activity resume. activity = de.dosofun.knowitall.Knowitall2Activity@44ede8f8
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][ARMAssembler( 60)] generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x3666d0:0x36678c] in 4662744 ns
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][ActivityManager( 60)] Displayed activity de.dosofun.knowitall/.Knowitall2Activity: 5509 ms (total 5509 ms)
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][TiAPI ( 301)] Start handleGraphRequest-Callback
[INFO][TiAPI ( 301)] Graph Request Callback Sucessful
行的原因:[INFO][TiAPI (301)] Logged In Facebook Account is : undefined 你可以看到代码没有等到调用的方法完成。
什么是必要的,这样我的代码才能按预期工作,如果我的代码很糟糕,什么是更好的方法(这不会让人难以阅读)
谢谢你的帮助