簡體   English   中英

將th子添加到jQuery post()

[英]Add throbber to jQuery post()

我發布ajax帖子時顯示出了不安。 以下是我的操作方式。 我想使代碼更簡潔。 我可以擴展$ .post這樣做嗎? 還有其他選擇嗎? 謝謝

//myThrobber is an object that displays a throbber in the center of the page
myThrobber.start();
$.post('somePage.php',myData,function (json){
    myThrobber.stop();
    //do whatever
    },'json');

這就是我寫包裹在$.post周圍的postThrob函數的方式。

$.fn.postThrob = function (throbber, url, data, callback) {
    throbber.start();
    $.post(url, data, function (response) {
        callback(response);
        throbber.stop();
    });
};

因此,現在您可以使用$.postThrob代替$.post 只需將您的throbber對象作為第一個參數傳入即可。

如果您不想傳入更麻煩的obj,則可以使用全局變量,盡管它不太理想。 只是不要在函數本身內部創建對象。

我仍然看不出這樣做的價值;(

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM