2012-11-06 102 views
-1

我有这个问题。 我有一个叫做functionA()的函数,它需要来自另一个函数functionB()的结果。javascript:等待退货

var globalVar=""; 
function functionA(){ 
    //... 
    functionB(); 
    //here i have to use the global variable (that is empty because functionB isn't finished) 
} 
function functionB(){ 
    //ajax request 
    globalVar=ajaxRequest.responseText; 
} 

我该怎么做让函数B完成befor继续执行functionA? 谢谢!

这是代码:

var ingredientiEsistenti=""; 
function ShowInserisciCommerciale() { 
    getElementiEsistenti(); 
    JSON.parse(ingredientiEsistenti); 
} 

function getElementiEsistenti(){ 

// prendo gli ingredienti esistenti. 
var url = "http://127.0.0.1:8080/Tesi/Ingredienti"; 
var xmlHttp = new XMLHttpRequest(); 
xmlHttp.open("POST", url, false); 
xmlHttp.send(null); 
xmlHttp.setRequestHeader("Content-type", 
     "application/x-www-form-urlencoded"); 
xmlHttp.onreadystatechange = function() { 
    if (xmlHttp.readyState == 4) // COMPLETED 
    { 
     if (xmlHttp.status == 200) // SUCCESSFUL 
     { 
      ingredientiEsistenti = xmlHttp.responseText; 
     } else { 

      alert("An error occurred while communicating with login server."); 
     } 
    } 
}; 
} 
+1

如果您可以直接控制XMLHttpRequest对象,则可以将其设置为不是异步调用。否则,我会咨询你用来做ajax调用的库,把它变成一个同步调用或修改'functionB()'下面的代码作为对async'functionB()'调用的回调。 –

+0

http://www.hunlock.com/blogs/Snippets:_Synchronous_AJAX – scrappedcola

回答

1

你有许多选择一个客场,不需要一个全局变量:

  • 移动你想要的代码,看看执行到onreadystatechange回调Ajax请求的,这样一来,就不会得到执行,直到接收到响应

  • 重新定义functionA,因此,它需要一个参数,可以让你跳过第一比特:

  • 使所述请求同步的,不推荐,虽然

  • 使用超时/间隔手动检查该请求的readyState(蛮力,不推荐使用要么)

  • 也许有一些工人挂羊头卖狗肉,可以做的伎俩,同样,你的具体情况


function functionA(skipDown) 
{ 
    skipDown = skipDown || false; 
    if (skipDown === false) 
    { 
     //doStuff 
     return functionB();//<-- call functionA(true); from the readystatechange callback 
    } 
    //this code will only be called if skipDown was passed 
} 
0

它是不可能有一个睡眠/ JavaScript中等待时的调用是异步的。您需要使用回调模式来执行此操作。

可以使XMLHttpRequest同步,但这可能会导致其他问题。它可以挂起浏览器,因为它会阻止所有其他操作的发生。所以如果你想显示一个加载动画,它很可能不会执行。

+0

除了你可以告诉你的XHR是同步的 –

+0

我该怎么做? – Martina

+0

@Marty - 将false传递给请求对象的打开函数。 ajaxRequest。打开(“GET”,url,false); – scrappedcola

0

您可以使您的AJAX请求同步。 https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Synchronous_and_Asynchronous_Requests

var request = new XMLHttpRequest(); 
// Last parameter makes it not asnychronous 
request.open('GET', 'http://www.mozilla.org/', false); 
request.send(null); 
// Won't get here until the network call finishes 
if (request.status === 200) { 
    console.log(request.responseText); 
} 

然而,这将阻止用户界面,同时等待服务器的响应,这是几乎从来没有你想要的东西。在这种情况下,您应该使用回调来处理结果。

下面是一个使用回调而不依赖于全局变量的示例。您应该始终运行从那些

function ShowInserisciCommerciale() { 
    getElementiEsistenti(function(responseText) { 
     JSON.parse(responseText); 
    }); 
} 

function getElementiEsistenti(successCallback){ 
    var url = "http://127.0.0.1:8080/Tesi/Ingredienti"; 
    var xmlHttp = new XMLHttpRequest(); 
    xmlHttp.open("POST", url, false); 
    xmlHttp.send(null); 
    xmlHttp.setRequestHeader("Content-type", 
      "application/x-www-form-urlencoded"); 
    xmlHttp.onreadystatechange = function() { 
     if (xmlHttp.readyState == 4) // COMPLETED 
     { 
      if (xmlHttp.status == 200) // SUCCESSFUL 
      { 
       successCallback(xmlHttp.responseText); 

      } else { 

       alert("An error occurred while communicating with login server."); 
      } 
     } 
    }; 
} 
+0

抱歉,什么是funB? – Martina

+0

这是你的'functionB'?让我举一个例子,你的代码 –

+0

现在我尝试这种方法... – Martina