English 中文(简体)
碎块功能的回归价值
原标题:Return value from parameter function in js

我想回头来谈谈反馈功能的价值,如下文所示。 不设法了解如何使其发挥作用(仅用于解释目的的执行)

console.log( hasLogin() );

function hasLogin(){
  FB.getLoginStatus(function(response) {
    if (response.session) {
        return true;
      } 
    else {
      return false;
  }
});

}

在实践中,我希望有行文记录仪,以复信。 届会确实存在。

感谢!

最佳回答

您没有,因为<代码>植被图/代码”设计成同步,结果根本无法取得。 最好的战略是 停留地:

function checkLogin(result_callback) {
    FB.getLoginStatus(function(response) {
         result_callback(!!response.session);
    }
}

check_login(function(result) {console.log(result);});

通常如何在 Java文中做到这一点。 当然,这迫使你重新考虑你的法典流。

问题回答

getLoginStatus is calling your anonymous function, and that function is returning true or false, not hasLogin. hasLogin is actually returning undefined. Since this is happening asynchronously, you ll need to do something like this:

FB.getLoginStatus(function(response) {
    console.log(response.session);
});

处理好象的电话。

您确实可以这样做,因为get CarloinStatus的运行时间不时,它为什么能提供回击功能。

你可以做的是:

function hasLogin(){
    FB.getLoginStatus(function(response) {
        if (response.session) {
            console.log("got session");
        } 
        else {
            console.log("got no session");
        }
    });
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签