繁体   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