簡體   English   中英

Javascript / jQuery:如何使該AJAX請求運行得更快?

[英]Javascript /jQuery: How can I make this AJAX request run faster?

我有這個AJAX請求,該請求連接到newsletter-signup.html文件,然后在返回的data變量中搜索字符串“ form-success”。 該字符串通過if語句中的(Django)模板應用,以標記表單成功。

以下是搜索整個數據的代碼段:

if (data.indexOf('form-success') > 0) {

有沒有更快的方法來搜索這些數據? 或者,也許算是一種更好的方法來確定返回的數據是否成功注冊?

在此先感謝您的幫助。 這是完整的代碼:

    $.ajax({ url: '/newsletter-signup', type: 'GET', data: {email: emailVal},  async: false,
        success: function(data){
            // Returning 'form-success' in our data string indicates the sign up worked
            if (data.indexOf('form-success') > 0) {
                // open the success message in a modal window
                happyModal.open({ content: thankYouContent });
            } else {
                // if not successful re-load the contents, firing our standard error message
                theBtn.removeClass('disabled');
                $('.login-panel').load('/newsletter-signup','email=' + encodeURIComponent(emailVal), function () {
                });
            }
        }
    });

我非常懷疑您在data.indexOf調用期間是否看到任何明顯的延遲。 您感覺到的延遲可能是ajax調用本身,這將花費您網絡上的所有時間。 您無法在代碼中做任何事情來加快速度。

您還可以使用async: false來進行呼叫,這意味着在大多數情況下,您會在進行呼叫時鎖定瀏覽器,這不會帶來令人愉悅的用戶體驗,並且可能會強調延遲。 我強烈建議您允許該請求與某種“一時”顯示異步進行,從而允許用戶在等待時使用其他選項卡,等等。

(旁注:請注意, async: false將在jQuery 2.0中消失。)

暫無
暫無

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

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