jquery getJSON函数计时问题

发布于 2024-09-12 22:15:58 字数 436 浏览 9 评论 0原文

我认为我的程序正在跳过 JSON 调用的结果。 是否可以在此处创建一个闭包函数或让程序等待 JSON 调用返回?

function username_not_duplicate(username) {
   var function_name = "get_username";
   var parameters = [username];
   var url = "/get_functions.php?function_name=" + function_name + "&parameters=" + parameters;
   $.getJSON(url, function(user_name) {
      if (user_name == true) {     
         return true;
      }
   });
   return false;
}

I think my program is skipping result of JSON call.
Is it possible to make a closure function here or make the program wait for JSON call to return?

function username_not_duplicate(username) {
   var function_name = "get_username";
   var parameters = [username];
   var url = "/get_functions.php?function_name=" + function_name + "¶meters=" + parameters;
   $.getJSON(url, function(user_name) {
      if (user_name == true) {     
         return true;
      }
   });
   return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

够运 2024-09-19 22:15:58

$.getJSON() API 调用是异步的。您可以通过以下方式使用 $.ajax() 使其同步:

function username_not_duplicate(username) {
   var function_name = "get_username";
   var parameters = [username];
   var url = "/get_functions.php?function_name=" + function_name + "¶meters=" + parameters;
   var rslt = false;
   $.ajax({
     async: false,
     url: url,
     dataType: "json",
     success: function(data) {
       if (data == true) {     
         rslt = true;
       }
     }
   });
   return rslt;
}

The $.getJSON() API call is asynchronous. You can make it synchronous by using $.ajax() this way:

function username_not_duplicate(username) {
   var function_name = "get_username";
   var parameters = [username];
   var url = "/get_functions.php?function_name=" + function_name + "¶meters=" + parameters;
   var rslt = false;
   $.ajax({
     async: false,
     url: url,
     dataType: "json",
     success: function(data) {
       if (data == true) {     
         rslt = true;
       }
     }
   });
   return rslt;
}
暮色兮凉城 2024-09-19 22:15:58

Drew 的答案近乎完美,只是少了一个括号和 IE 的逗号。

function username_not_duplicate(username) {
   var function_name = "get_username"; 
   var parameters = [username]; 
   var url = "camps/includes/get_functions.php?function_name=" + function_name + "¶meters=" + parameters;
   var rslt = false; 
   $.ajax({ 
         async: false, 
         url: url, 
         dataType: "json", 
         success: function(data) {
           if (data == true) {                   
             rslt = true; 
           }
        }, 
    });
    return rslt; 
}

Drew's answer is nearly perfect, just missing one bracket and comma for IE.

function username_not_duplicate(username) {
   var function_name = "get_username"; 
   var parameters = [username]; 
   var url = "camps/includes/get_functions.php?function_name=" + function_name + "¶meters=" + parameters;
   var rslt = false; 
   $.ajax({ 
         async: false, 
         url: url, 
         dataType: "json", 
         success: function(data) {
           if (data == true) {                   
             rslt = true; 
           }
        }, 
    });
    return rslt; 
}
别靠近我心 2024-09-19 22:15:58

另一种选择是使用回调函数并将其传递给执行 getJSON 的函数,如下所示:

//this is the function that executes my getJSON
//callback is the name of my callback function
function getMyData( callback ){

  //here you do your getJSON call
  $.getJSON(url, function(items){

    //do your stuff here

    //call your function here (at the end)
    callback();

  });

}

这样您的回调函数将在 getJSON 调用结束时被调用。

Another choice is to use a call back function and pass it to the function that executes the getJSON as:

//this is the function that executes my getJSON
//callback is the name of my callback function
function getMyData( callback ){

  //here you do your getJSON call
  $.getJSON(url, function(items){

    //do your stuff here

    //call your function here (at the end)
    callback();

  });

}

This way your callback function will be called at the end of the getJSON call.

深海夜未眠 2024-09-19 22:15:58

是的,username_not_duplicate 只是立即返回 false,因为 getJSON 是异步的(即非阻塞)。 return true 语句仅从响应处理程序返回 true
通常,您不应该执行您想要实现的此类阻塞调用。我想您可以考虑在全球某个地方记住请求的状态。

Yeap, username_not_duplicate just returns false immediately because getJSON is asynchronous (ie non-blocking). The return true statement just returns true from the response handler.
Normally, you shouldn't do such a blocking calls you're trying to achieve. I suppose you can consider remembering of a state of the request somewhere globally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文