[英]Can I add getJSON's done()-functions by callback?
在此代碼上:
function hello(){
alert("hello");
}
function hi(){
alert("hi");
}
jQuery.getJSON('foo.bar',function(data){
if (data.foobar) {
this.done(hello);
}
alert('callback done');
}).done(hi);
並假設這從foo.bar
返回
{"foobar":true}
我喜歡按以下順序發送警報:
但是我不能在success
回調中添加done()
函數。
您是否暗示我如何在success
回調中添加done()
函數?
jqXHR
對象是回調函數的第三個參數:
jQuery.getJSON('foo.bar', function(data, textStatus, jqXHR) {
if (data.foobar) {
jqXHR.done(hello);
}
alert('calback done');
}).done(hi);
我不認為您應該首先嘗試將回調和done()
處理程序結合起來,更不用說像這樣將它們交織在一起了。 只需使用一個done()
處理函數即可:
jQuery.getJSON('foo.bar').done(function (data) {
hi();
if (data.foobar) {
hello();
}
});
或者,如果您擔心hi()
的失敗會阻止其余代碼執行,則可以將它們分別傳遞給done()
:
jQuery.getJSON('foo.bar').done(hi, function (data) {
if (data.foobar) {
hello();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.