简体   繁体   English

如何在 L.tileLayer 上为 tileerror 设置超时?

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

I am getting layers from a WMS service, but sometimes it happens that the connection to the server times out.我从 WMS 服务获取图层,但有时会发生与服务器的连接超时。 In those cases, the application hangs for a long time waiting for a NET_ERR, but the timeout is too long.在这些情况下,应用程序挂起很长时间等待 NET_ERR,但超时时间太长。

I am catching the error with "tileerror":我用“tileerror”捕获错误:

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

How can I shorten the default timeout and take the corrective action?如何缩短默认超时时间并采取纠正措施?

How can I shorten the default timeout and take the corrective action?如何缩短默认超时时间并采取纠正措施?

You can't.你不能。 It's browser-specific, and it doesn't have an API.它是特定于浏览器的,并且没有 API。

You can, however, create your own subclass of L.TileLayer and add some extra logic.但是,您可以创建自己的L.TileLayer子类并添加一些额外的逻辑。 See these lines in the default implementation of L.TileLayer.prototype.createTile :请参阅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));

You could trigger a shorter timeout with something like:您可以使用以下内容触发更短的超时:

    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();
    });

There are probably some race conditions that I cannot foresee right now, but that's the general idea.可能有一些我现在无法预见的竞争条件,但这是总体思路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM