English 中文(简体)
返回本地变量或获取结果
原标题:Return either local variable or GET results

我想还你一美元

换句话说,如果 x 是真实的, 那么返回 x, 其它则执行 Get 调用, 并返回服务器提供的值 。

我的尝试列于下文(最好是采用返回 x y 格式,也许使用匿名功能? 而不是如果/当时的功能)。

问题是我从$. get 函数返回的回报 似乎跟我预期的不一样

请你解释一下这是怎么回事

谢谢 谢谢

$(function(){

  function test(x,y) {
    if(x==true) {return true;}
    else{
      //test.php is echo($_GET[ y ]==123);
      $.get( ajax.php ,{ y :y},function (status) {return status;});
    }
  }

  alert(test(false,123));

});
最佳回答

如果您在使用 jQuery 1. 5 或以后, < a href=" http://api.jquery.com/cortion/ deferred-object/" rel="no follow" > Deferred 和 < a href="http://api.jquery.com/Types/#Promoise" rel=“nofollow" > Promise 是这类事情的朋友。您调用 AAAAX 调回什么是承诺物,您可以通过.done ()、.fail () 和.then () 附加函数。

然而,正如这个极好的推迟/承诺和所有这些东西(http://www.erichynds.com/jquery/using-deferreds-in-jquery/ )指出的,你还可以使用$.wait () 的能力来处理一个不承诺自动缓冲的价值。

$.when(getToken()).done(
  function (token) {
    // do something with the token, which may or may not have been 
    // retrieved from the remote service
  }
);

既可以处理获取缓存值回来, 也可以处理没有问题的承诺 :

function getToken() {
  // Return either the cached value or a jQuery Promise. If $.when() gets the
  // cached value it will immediately realize that you didn t give it a
  // promise and it will instead create a jQuery Deferred to return and
  // .resolve() it using the value it did get. Thus, either way what
  // comes out of the function is something .when() can deal with and call a function.
  if (this.cache["token"]) {
    return this.cache["token"];
  } else {
    return $.get(" ... some url ... ");
  }
};
问题回答

暂无回答




相关问题
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.

热门标签