-
-
Save vioKamran/f3b05e1dd789f72f6df30746f06761bf to your computer and use it in GitHub Desktop.
jQuery.getJSON abstraction to cache data to localStorage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var getCachedJSON = function (url, callback) { | |
var cachedData = window.localStorage[url]; | |
if (cachedData) { | |
log('Data already cached, returning from cache:', url); | |
callback(JSON.parse(cachedData)); | |
} else { | |
$.getJSON(url, function (data) { | |
log('Fetched data, saving to cache:', url); | |
window.localStorage[url] = JSON.stringify(data); | |
callback(data); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This best