Skip to content

Instantly share code, notes, and snippets.

@jamesmusgrave
Forked from kpuputti/gist:1040118
Last active December 27, 2015 11:59
Show Gist options
  • Save jamesmusgrave/7322303 to your computer and use it in GitHub Desktop.
Save jamesmusgrave/7322303 to your computer and use it in GitHub Desktop.
Cached JSON via jQuery
var getCachedJSON = function (url) {
var deferred = new $.Deferred();
var cachedData = window.localStorage[url];
if (cachedData) {
log('Data already cached, returning from cache:', url);
deferred.resolve(JSON.parse(cachedData));
} else {
$.getJSON(url, function(data) {
log('Fetched data, saving to cache:', url);
window.localStorage[url] = JSON.stringify(data);
deferred.resolve(data);
});
}
return deferred.promise();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment