Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
handle errors when fetching data
  • Loading branch information
Jeff Balogh committed Mar 12, 2011
1 parent c0f5260 commit 7958adb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
2 changes: 1 addition & 1 deletion media/counter.js
Expand Up @@ -19,6 +19,6 @@ function initCounter() {

vast.animate.over(response.interval * 1000, drawCounter, this,
{after: glow.count.playNext});
setTimeout(glow.fetchCount, response.interval * 500);
glow.fetchCount(response.interval);
};
}
65 changes: 55 additions & 10 deletions media/glow.js
Expand Up @@ -9,22 +9,67 @@ glow.data = {

var ROOT = "data/json/";

glow.init = function() {
glow.fetchCount = function() {
$.getJSON(ROOT + glow.data.count.next.next, function(r) {
glow.data.count.next = r;
glow.data.sector.next = r.next.replace('count', 'daisy');
});
// Load the next file of json data before timeout seconds have elapsed.
glow.fetchCount = function(timeout) {
getData(glow.data.map.next.next, timeout, function(r) {
glow.data.count.next = r;
// We don't request data.sector until the page flips, but
// but we want to be ready with the right url.
glow.data.sector.next = r.next.replace('count', 'daisy');
};
};

glow.fetchMap = function() {
$.getJSON(ROOT + glow.data.map.next.next, function(r) {
glow.data.map.next = r;
});
// Load the next file of json data before timeout seconds have elapsed.
glow.fetchMap = function(timeout) {
getData(glow.data.map.next.next, timeout, function(r) {
glow.data.map.next = r;
};
};

// $.getJSON with an error parameter.
var getJSON = function(url, success, error) {
$.ajax({url: ROOT + url, dataType: 'json',
success: success, error: error});
};

// Reload the page if we couldn't fetch any data after a few retries. This
// drops us back into the loading screen with error messaging.
var fetchFailure = function() {
window.location.reload();
};

// We have timeout seconds to pull the next chunk of data.
// Start by waiting timeout/2 seconds before the first attempt.
// If that fails, retry every 5 seconds until we run out of time.
var getData = function(url, timeout, success) {
var error = setTimeout(fetchFailure, timeout * 1000),
remaining = timeout / 2;

var success_ = function(data, textStatus, xhr) {
dbg('Success', url);
clearTimeout(error);
success(data, textStatus, xhr);
};

var fetch = function(timeout) {
setTimeout(function() {
getJSON(url, success_, function() {
dbg('Error, retrying', url);
if (!glow.stop) {
remaining -= 5;
fetch(5);
}
});
}, timeout * 1000);
};
fetch(remaining);
};


glow.init = function() {
$.getJSON(ROOT + glow.time + "/count.json", function(r) {
glow.data.count.next = r;
glow.data.sector.next = ROOT + glow.time + "/daisy.json";
glow.count.playNext();
glow.bar.playNext();

Expand Down
2 changes: 1 addition & 1 deletion media/map.js
Expand Up @@ -55,7 +55,7 @@ function initMap() {
}
vast.animate.over(response.interval * 1000, drawPings, this,
{after: glow.map.playNext});
setTimeout(glow.fetchMap, response.interval * 500);
glow.fetchMap(response.interval);
};

$(window).resize(vast.debounce(function() {
Expand Down

0 comments on commit 7958adb

Please sign in to comment.