Jquery延迟回调奇怪 [英] Jquery deferred call back oddness
问题描述
jsfiddle.net/austinbv/QVujr/rel =nofollow> http://jsfiddle.net/austinbv/QVujr/
get_each_total = function(callback){
var requests;
requests = [];
var url;
url =http://otter.topsy.com/search.js?callback=?&apikey=38A260E9D12A4908B1AF9184B691131&q=justin+bieber&window=d;
return requests.push($。getJSON(url,function(data){
}));
return $ .when.apply($,requests).then(function(){
callback();
},function(){
return alert与远程库通信时出错,请在几个中重试);
});
};
get_each_total_broken = function(callback){
var requests;
requests = [];
var url;
url =http://otter.topsy.com/hjhkl/sehjkhhkjhkarch.js?callback=?&apikey=38A260E9D12A4908B1AF9184B691131&q=justin+bieber&window=d;
return requests.push($。getJSON(url,function(data){
}));
return $ .when.apply($,requests).then(function(){
callback();
},function(){
return alert与远程库通信时出错,请在几个中重试);
});
};
$(function(){
get_each_total(alert(success));
get_each_total_broken(alert(fail));
});
而这不
http://jsfiddle.net/austinbv/wzve6/
get_each_total = function(callback){
var requests;
requests = [];
var url;
url =http://otter.topsy.com/search.js?callback=?&apikey=38A260E9D12A4908B1AF9184B691131&q=justin+bieber&window=d;
return requests.push($。getJSON(url,function(data){
}));
return $ .when.apply($,requests).then(function(){
callback();
},function(){
return alert与远程库通信时出错,请在几个中重试);
});
};
get_each_total_broken = function(callback){
var requests;
requests = [];
var url;
url =http://otter.topsy.com/hjhkl/sehjkhhkjhkarch.js?callback=?&apikey=38A260E9D12A4908B1AF9184B691131&q=justin+bieber&window=d;
return requests.push($。getJSON(url,function(data){
}));
return $ .when.apply($,requests).then(function(){
callback();
},function(){
return alert与远程库通信时出错,请在几个中重试);
});
};
$(function(){
get_each_total(function(){alert(success)});
get_each_total_broken );
});
,你可以看到唯一的区别是最后两行,其中一个匿名函数包装回调。任何洞察都会很好。
这段代码:
return requests.push($。getJSON(url,function(data){
}));
退出您的函数。 回调
从未被调用。
当你说唯一的区别是一个包装回调
的匿名函数,这意味着你还在第一个版本的代码中传递一个函数。这不是真的;你试图传入 alert('whatever');
正在返回 undefined
进一步说明:
get_each_total
,& get_each_total_broken
)期望参数是一个函数。这是明显的,你试图调用它作为一个函数后来在你的代码( callback()
)。但是,此行: get_each_total(alert(success));
不会将函数传递给 get_each_total
。它等价于以下:
var returnedFromAlert = alert(success);
get_each_total(returnedFromAlert);
因此,基本上你不会将任何东西传递到 get_each_total
函数。在调用
get_each_total
之前,您会立即收到成功
提醒。
I was playing around with call backs and deferred functions in jQuery and was wondering if anyone could tell me why this works
http://jsfiddle.net/austinbv/QVujr/
get_each_total = function(callback) {
var requests;
requests = [];
var url;
url = "http://otter.topsy.com/search.js?callback=?&apikey=38A260E9D12A4908B1AF9184B691131&q=justin+bieber&window=d";
return requests.push($.getJSON(url, function(data) {
}));
return $.when.apply($, requests).then(function() {
callback();
}, function() {
return alert("There was an error communicating with a remote library, try again in a few");
});
};
get_each_total_broken = function(callback) {
var requests;
requests = [];
var url;
url = "http://otter.topsy.com/hjhkl/sehjkhhkjhkarch.js?callback=?&apikey=38A260E9D12A4908B1AF9184B691131&q=justin+bieber&window=d";
return requests.push($.getJSON(url, function(data) {
}));
return $.when.apply($, requests).then(function() {
callback();
}, function() {
return alert("There was an error communicating with a remote library, try again in a few");
});
};
$(function () {
get_each_total(alert("success"));
get_each_total_broken(alert("fail"));
});
and this does not
http://jsfiddle.net/austinbv/wzve6/
get_each_total = function(callback) {
var requests;
requests = [];
var url;
url = "http://otter.topsy.com/search.js?callback=?&apikey=38A260E9D12A4908B1AF9184B691131&q=justin+bieber&window=d";
return requests.push($.getJSON(url, function(data) {
}));
return $.when.apply($, requests).then(function() {
callback();
}, function() {
return alert("There was an error communicating with a remote library, try again in a few");
});
};
get_each_total_broken = function(callback) {
var requests;
requests = [];
var url;
url = "http://otter.topsy.com/hjhkl/sehjkhhkjhkarch.js?callback=?&apikey=38A260E9D12A4908B1AF9184B691131&q=justin+bieber&window=d";
return requests.push($.getJSON(url, function(data) {
}));
return $.when.apply($, requests).then(function() {
callback();
}, function() {
return alert("There was an error communicating with a remote library, try again in a few");
});
};
$(function () {
get_each_total(function () { alert("success")});
get_each_total_broken(function () {alert("fail")});
});
as you can see the only difference is in the last two lines, where an anonymous function wraps the callback. Any insight would be nice.
This piece of code:
return requests.push($.getJSON(url, function(data) {
}));
exits out of your function. The callback
is never called.
P.S. When you're saying the only difference is an anonymous function wrapping the callback
, you imply that you're also passing a function in the first version of your code. That is not true; you're trying to pass in whatever alert('whatever');
is returning, which is undefined
!
Further explanation:
Both of your functions (get_each_total
, & get_each_total_broken
) expect the parameter to be a function. This is evident by you trying to call it as a function later on in your code (callback()
). However, this line:
get_each_total(alert("success"));
does not pass a function to get_each_total
. It is equivalent to the following:
var returnedFromAlert = alert("success");
get_each_total(returnedFromAlert);
So, basically, you're not passing anything into your get_each_total
function. You get a success
alert right away, before get_each_total
has been called.
这篇关于Jquery延迟回调奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!