jquery中done與then方法區別

2021-10-07 23:23:27 字數 1668 閱讀 5884

jquery的deferred物件的done方法和then方法都能實現鏈式呼叫,但是他們的作用是有區別的,

then方法中如果你傳遞的方法有返回值,那麼他會傳遞給下乙個鏈式呼叫的方法。

而done方法與此相反,你傳遞的方法就算有返回值,done方法也不會把你的返回值傳給下乙個鏈式呼叫的方法的。即:

done返回當前的的deferred object,callback的返回值不會被傳遞;

then返回乙個新的deferred object,callback的返回值會被傳遞(參考jquery的pipe屬性)給新的callback.

var defer = jquery.

deferred()

; defer.

done

(function

(a, b)).

done

(function

(result)).

then

(function

(a, b)).

done

(function

(result)).

then

(function

(a, b)).

done

(function

(result));

defer.

resolve(2

,3);

//案例1:

/** * run1

* 非同步過程1

* 我是資料1

* run2

* 非同步過程2

* 我是資料2

* 非同步過程3

* 我是資料3

* */

function

run1()

,1000)}

)}function

run2()

,1000)}

)}function

run3()

,1000)}

)}run1()

.then

(function

(data)).

then

(function

(data)).

then

(function

(data)

)

// 案例2

newpromise

(function

(resolve, reject)).

then

(function

(result));

}).then

(function

(result));

}).then

(function

(result)

);

// 案例3

let run =

async

function()

function

run1()

,2000)}

)}function

run2()

,2000)}

)}run(

)

案例參照:

jquery的done和then區別

jquery的deferred物件的done方法和then方法都能實現鏈式呼叫,但是他們的作用是有區別的,then方法中如果你傳遞的方法有返回值,那麼他會傳遞給下乙個鏈式呼叫的方法。而done方法與此相反,你傳遞的方法就算有返回值,done方法也不會把你的返回值傳給下乙個鏈式呼叫的方法的,話不多說,...

在EditText中開啟軟鍵盤的 Done 按鈕

整理自 在edittext中,可以使用setimeoptions 方法來來開啟軟鍵盤的 done 按鈕。示例 如下 edittext.setimeoptions editorinfo.ime action done 介面如下 按下 done 按鈕的預設行為是關閉軟鍵盤,但是我們可以通過 editte...

jQuery中this與 this 的區別

js textbox hover function function 這裡的this其實是乙個html 元素 textbox textbox有text屬性,所以這樣寫是完全沒有什麼問題的。但是如果將this換成 this 就不是那回事了,error 報了。this與 this 的區別在此。error...