0

我有一个执行某些任务的函数。在函数内部我有一个 ajax 调用。如果从 ajaxcall 获得的响应为真,则该函数应该继续执行任务的其余部分。否则它应该在该点本身停止。但是上面所说的事情并没有发生,而是该函数独立于 ajax 调用执行。请帮帮我

function f1(id)
{
var test= id
var url="contentserver?pagename=mandatory@id"=+id;
var ajax=new Ajaxrequest(url,validatecallback);
ajax.doGet();
if(id==true){
return;
}
........(some code which has to carried out after the ajax call)
}
function validatecallback
{
this function gets the response for the above mentioned ajaxcall
we are setting a global variable(i.e id) here so that we can use that we can retrieve that in function f1
}
4

2 回答 2

0

The function "f1" has a formal parameter called "id"; that's the variable it will test in the statement "if(id==true)". While it may be true that there is also a global variable called "id", and that the callback function will access and modify it, it is a different variable. A modification to it will not affect the test in f1.

于 2010-09-15T18:54:38.597 回答
0

See my answer to this related question: How can I write an async method in JavaScript when posting or looping?

Basically you have to change your thinking. It needs to instead be something like:

function f1(id) {
  var test= id
  var url="contentserver?pagename=mandatory@id"=+id;
  var ajax=new Ajaxrequest(url,function (r) {
      validatecallback(r)
      if(id==true){
        return;
      }
      ........(some code which has to carried out after the ajax call)
  });
  ajax.doGet();
}
于 2010-09-15T18:56:50.183 回答