Javascript设置变量值

发布于 2024-09-18 01:57:31 字数 351 浏览 4 评论 0原文

我想从 if else 块设置 stat 的值,但是当我设置它并提醒它时,它对我说“未定义”。如何设置 stat 的值?这是我的代码。

deleteComment = function(postId){
  var stat = "Don't Know";
  FB.api(postId, 'delete', function(response) {
    if (!response || response.error) {
      stat = "Error2";
    } else {
      stat = "Deleted"
    }
  });

  alert(stat);
};

提前致谢

I want to set the value of stat from if else block but when I set it and alert it then it says to me "undefined". How can I set the value of stat. Here is my code.

deleteComment = function(postId){
  var stat = "Don't Know";
  FB.api(postId, 'delete', function(response) {
    if (!response || response.error) {
      stat = "Error2";
    } else {
      stat = "Deleted"
    }
  });

  alert(stat);
};

Thanks in Advance

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

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

发布评论

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

评论(3

瞳孔里扚悲伤 2024-09-25 01:57:31

您的函数调用是异步的。这意味着,代码中的 alert() 在 HTTP 请求尚未返回时运行。

在回调函数中执行警报,因为只有这样它才有值:

deleteComment = function(postId){
  FB.api(postId, 'delete', function(response) {
    var stat = "Don't Know";
    if (!response || response.error) {
      stat = "Error2";
    } else {
      stat = "Deleted";
    }
    alert(stat);
  });
}

Your function call is asynchronuous. This means, the alert() in your code runs when the HTTP request has not even returned yet.

Do the alert in the callback function, because only then it has a value:

deleteComment = function(postId){
  FB.api(postId, 'delete', function(response) {
    var stat = "Don't Know";
    if (!response || response.error) {
      stat = "Error2";
    } else {
      stat = "Deleted";
    }
    alert(stat);
  });
}
难如初 2024-09-25 01:57:31

Facebook API 是异步,这意味着您传递给 FP 的回调函数。 api 调用稍后会在 API 调用完成后进行,但您的警报将在您调用 FB.api 后立即运行,这当然意味着回调函数尚未运行因此 stat 仍然是不知道

要使其正常工作,您必须将 alert 放入回调中:

deleteComment = function(postId){


    var stat = "Don't Know";

    // call is made...
    FB.api(postId, 'delete', function(response) {

        // if we land here, the callback has been called
        if (!response || response.error) {
            stat = "Error2";

        } else { 
            stat = "Deleted" 
        }
        alert(stat); // now - inside the callback - stat is actually set to the new value
   });

   // but execution continues
}

The Facebook API is asynchronous, that means the callback function you pass to the FP.api call will later, when the API call has finished, but your alert will run immediately after you made the call to FB.api which of course means that the callback function did not yet run and therefore stat is still Don't Know.

To make it work, you have to put the alert inside your callback:

deleteComment = function(postId){


    var stat = "Don't Know";

    // call is made...
    FB.api(postId, 'delete', function(response) {

        // if we land here, the callback has been called
        if (!response || response.error) {
            stat = "Error2";

        } else { 
            stat = "Deleted" 
        }
        alert(stat); // now - inside the callback - stat is actually set to the new value
   });

   // but execution continues
}
鯉魚旗 2024-09-25 01:57:31

您必须将警报(或其他内容)带入异步回调中:

deleteComment = function(postId){
  var stat = "Don't Know";
  FB.api(postId, 'delete', function(response) {
    if (!response || response.error) {
        stat = "Error2";
    } else {
        stat = "Deleted"
    }
    alert(stat);
  });
}

当您调用 API 时,它会立即返回。因此,如果您在外面收到警报,系统会立即呼叫。然后,稍后,您的回调(作为第三个参数传递的函数)被调用。

编辑:您无法从deleteComment返回stat。相反,做:

deleteComment = function(postId, callback){
  FB.api(postId, 'delete', function(response) {
    if (!response || response.error) {
        stat = "Error2";
    } else {
        stat = "Deleted"
    }
    callback(stat);
  });
}

你可以这样称呼它:

deleteComment(postid, function(stat)
{
  // use stat
});

You have to bring the alert (or whatever) into the async callback:

deleteComment = function(postId){
  var stat = "Don't Know";
  FB.api(postId, 'delete', function(response) {
    if (!response || response.error) {
        stat = "Error2";
    } else {
        stat = "Deleted"
    }
    alert(stat);
  });
}

When you call the API, it returns immediately. Thus, if you have the alert outside, it is called immediately. Then, later, your callback (the function you pass as the third parameter) is called.

EDIT: You can't return stat from deleteComment. Instead, do:

deleteComment = function(postId, callback){
  FB.api(postId, 'delete', function(response) {
    if (!response || response.error) {
        stat = "Error2";
    } else {
        stat = "Deleted"
    }
    callback(stat);
  });
}

You could call this like:

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