Created
September 1, 2013 10:02
-
-
Save captn3m0/6403443 to your computer and use it in GitHub Desktop.
JQuery JSON fetch function with cache support ($.getJSONCache)
This file contains hidden or 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
| /** | |
| * Uses localStorage for cache and if misses | |
| * uses getJSON to get content | |
| */ | |
| $.getJSONCache=function(url,data,success){ | |
| data = data || {} | |
| var key = url + '?' + $.param(data); | |
| var value = localStorage.getItem(key); | |
| if(value) | |
| { | |
| data = JSON.parse(value); | |
| if($.isFunction(success)) | |
| success(data); | |
| } | |
| else{ | |
| $.getJSON(url,data,function(data){ | |
| localStorage.setItem(key,JSON.stringify(data)); | |
| if($.isFunction(success)) | |
| success.apply(this,[data]); | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment