2017-04-06 69 views
0

我已經實現了一個jQuery Bootgrid。我想管理顯示和隱藏我已經實現的加載器。jQuery Bootgrid如何檢查bootgrid是否仍在加載

分別顯示和隱藏加載程序$("#ajaxLoader").show();和$(「#ajaxLoader」)。hide();`。

我有我的Bootgrid初始化實現如下:

var grid = $('#grid').on("load.rs.jquery.bootgrid", function() { 
    $("#ajaxLoader").show(); 
}).bootgrid({ 
    ajax: true, 
    ajaxSettings: { 
     method: "GET", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     cache: false 
    }, 
    searchSettings: { 
     delay: 500, 
    }, 
    url: myURL, 
    rowCount: [5, 10, 50, 75, 100, 200, -1] 
}).on("loaded.rs.jquery.bootgrid", function (e) { 
    $("#ajaxLoader").hide(); 
}); 

現在這工作正常,但我想優化它在兩個方面:

  1. Bootgrid的搜索功能似乎有它自己的裝載機,所以有沒有辦法爲了不顯示我的ajax加載程序,如果搜索發生?

  2. 我的裝載器彈出並快速隱藏短的查詢。有什麼辦法可以推遲發生?

解決選2可能會解決方案1在它自己的,但無論如何這裏是我試過關於選項2

我試過setTimeout(function() { $("#ajaxLoader").show(); }, 1000);,但那麼它永遠不會被再次隱藏。我通過使用setTimeout(function() { $("#ajaxLoader").hide(); }, 1000)解決了這個問題,但它閃爍顯示並不必要地隱藏。

所以理想情況下,我需要檢查Bootgrid是否在我應用該函數之前仍在加載。

我該如何解決我的加載問題?

編輯:

我也試着設置loading變量,其中對我load方法我說:

loading = true; 
setTimeout(function() { if (loading) { $("#ajaxLoader").show(); }}, 1000); 

和我loaded事件如下:

setTimeout(function() { $("#ajaxLoader").hide(); }, loaderDelay); 

這仍然給我同樣的閃爍錯誤。

回答

0

我解決它使用clearTimeout()功能是這樣的:

var showLoader; 

var grid = $('#grid').on("load.rs.jquery.bootgrid", function() { 
    showLoader = setTimeout(function() { $("#ajaxLoader").show(); }, 1000); 
}).bootgrid({ 
    ajax: true, 
    ajaxSettings: { 
     method: "GET", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     cache: false 
    }, 
    searchSettings: { 
     clearTimeout(showLoader); 
     delay: 500, 
    }, 
    url: myURL, 
    rowCount: [5, 10, 50, 75, 100, 200, -1] 
}).on("loaded.rs.jquery.bootgrid", function (e) { 
    $("#ajaxLoader").hide(); 
});