improve refresh handling

- increase timeout on zoom/move end to 0.8 seconds
- prevent repeated refreshes faster than every 5 seconds
- delay initial refresh on load - should prevent immediate refresh after page load

fix #200
This commit is contained in:
Jon Atkins
2013-05-01 01:57:02 +01:00
parent 19638dfc39
commit 308459178d
3 changed files with 33 additions and 11 deletions

View File

@ -226,14 +226,16 @@ window.setupMap = function() {
// update map hooks
map.on('movestart zoomstart', window.requests.abort);
map.on('moveend zoomend', function() { window.startRefreshTimeout(500) });
// run once on init
window.requestData();
window.startRefreshTimeout();
map.on('moveend zoomend', function() { console.log('map moveend'); window.startRefreshTimeout(ON_MOVE_REFRESH*1000) });
window.addResumeFunction(window.requestData);
window.requests.addRefreshFunction(window.requestData);
// start the refresh process with a small timeout, so the first data request happens quickly
// (the code originally called the request function directly, and triggered a normal delay for the nxt refresh.
// however, the moveend/zoomend gets triggered on map load, causing a duplicate refresh. this helps prevent that
window.startRefreshTimeout(ON_MOVE_REFRESH*1000);
};
// renders player details into the website. Since the player info is

View File

@ -8,6 +8,10 @@ window.failedRequestCount = 0;
window.requests = function() {}
//time of last refresh
window.requests._lastRefreshTime = 0;
window.requests._quickRefreshPending = false;
window.requests.add = function(ajax) {
window.activeRequests.push(ajax);
renderUpdateStatus();
@ -38,17 +42,19 @@ window.renderUpdateStatus = function() {
if(mapRunsUserAction)
t += 'paused during interaction';
else if(isIdle())
t += '<span style="color:red">Idle, not updating.</span>';
t += '<span style="color:#888">Idle, not updating.</span>';
else if(window.activeRequests.length > 0)
t += window.activeRequests.length + ' requests running.';
else if(window.requests._quickRefreshPending)
t += 'refreshing...';
else
t += 'Up to date.';
if(renderLimitReached())
t += ' <span style="color:red" class="help" title="Can only render so much before it gets unbearably slow. Not all entities are shown. Zoom in or increase the limit (search for MAX_DRAWN_*).">RENDER LIMIT</span> '
t += ' <span style="color:#f66" class="help" title="Can only render so much before it gets unbearably slow. Not all entities are shown. Zoom in or increase the limit (search for MAX_DRAWN_*).">RENDER LIMIT</span> '
if(window.failedRequestCount > 0)
t += ' <span style="color:red">' + window.failedRequestCount + ' failed</span>.'
t += ' <span style="color:#f66">' + window.failedRequestCount + ' failed</span>.'
t += '<br/>(';
var minlvl = getMinPortalLevel();
@ -78,19 +84,28 @@ window.startRefreshTimeout = function(override) {
if(refreshTimeout) clearTimeout(refreshTimeout);
var t = 0;
if(override) {
window.requests._quickRefreshPending = true;
t = override;
//ensure override can't cause too fast a refresh if repeatedly used (e.g. lots of scrolling/zooming)
timeSinceLastRefresh = new Date().getTime()-window.requests._lastRefreshTime;
if(timeSinceLastRefresh < 0) timeSinceLastRefresh = 0; //in case of clock adjustments
if(timeSinceLastRefresh < MINIMUM_OVERRIDE_REFRESH*1000)
t = (MINIMUM_OVERRIDE_REFRESH*1000-timeSinceLastRefresh);
} else {
window.requests._quickRefreshPending = false;
t = REFRESH*1000;
var adj = ZOOM_LEVEL_ADJ * (18 - window.map.getZoom());
if(adj > 0) t += adj*1000;
}
var next = new Date(new Date().getTime() + t).toLocaleTimeString();
console.log('planned refresh: ' + next);
console.log('planned refresh in ' + (t/1000) + ' seconds, at ' + next);
refreshTimeout = setTimeout(window.requests._callOnRefreshFunctions, t);
renderUpdateStatus();
}
window.requests._onRefreshFunctions = [];
window.requests._callOnRefreshFunctions = function() {
console.log('running refresh at ' + new Date().toLocaleTimeString());
startRefreshTimeout();
if(isIdle()) {
@ -101,6 +116,9 @@ window.requests._callOnRefreshFunctions = function() {
console.log('refreshing');
//store the timestamp of this refresh
window.requests._lastRefreshTime = new Date().getTime();
$.each(window.requests._onRefreshFunctions, function(ind, f) {
f();
});
@ -121,4 +139,4 @@ window.requests.isLastRequest = function(action) {
}
});
return result;
}
}