簡體   English   中英

如何在 L.tileLayer 上為 tileerror 設置超時?

[英]How to set a timeout to tileerror on L.tileLayer?

我從 WMS 服務獲取圖層,但有時會發生與服務器的連接超時。 在這些情況下,應用程序掛起很長時間等待 NET_ERR,但超時時間太長。

我用“tileerror”捕獲錯誤:

myLayer.on('tileerror', function(error, tile) {
    console.log(error);
    console.log(tile);
    switchToBackupServer();
    });

如何縮短默認超時時間並采取糾正措施?

如何縮短默認超時時間並采取糾正措施?

你不能。 它是特定於瀏覽器的,並且沒有 API。

但是,您可以創建自己的L.TileLayer子類並添加一些額外的邏輯。 請參閱L.TileLayer.prototype.createTile的默認實現中的這些行:

    DomEvent.on(tile, 'load', Util.bind(this._tileOnLoad, this, done, tile));
    DomEvent.on(tile, 'error', Util.bind(this._tileOnError, this, done, tile));

您可以使用以下內容觸發更短的超時:

    var loadCallback = Util.bind(this._tileOnLoad, this, done, tile);
    var errorCallback = Util.bind(this._tileOnError, this, done, tile);

    DomEvent.on(tile, 'load', loadCallback);
    DomEvent.on(tile, 'error', errorCallback);

    setTimeout(function(){
        // Do nothing if the tile has already been loaded successfully
        if (tile.loaded) return;

        // Prevent any further events from triggering
        DomEvent.off(tile, 'load', loadCallback);
        DomEvent.off(tile, 'error', errorCallback);

        // Trigger the error
        errorCallback();
    });

可能有一些我現在無法預見的競爭條件,但這是總體思路。

暫無
暫無

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

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