如何为 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();
});
如何缩短默认超时并采取纠正措施?
How can I shorten the default timeout and take the corrective action?
你不能。它是特定于浏览器的,并且没有 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();
});
可能存在一些我现在无法预见的竞争条件,但这是一般的想法。
我正在从 WMS 服务获取图层,但有时会发生与服务器的连接超时。在那些情况下,应用程序挂起很长时间等待 NET_ERR,但超时时间太长。
我正在用 "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?
你不能。它是特定于浏览器的,并且没有 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();
});
可能存在一些我现在无法预见的竞争条件,但这是一般的想法。