/    Sign up×
Community /Pin to ProfileBookmark

Need help with local storage

I am working on a project for school and one of the requirements is to prevent additional GET requests after the data has been cached once in local storage. Below is my code. I have been trying numerous things with no luck. Hoping someone can help!

[code]
function getFoodMenuData() {
var url = ‘http://localhost:8888/Tom_Carp_Final_Project/Chorizios/foodMenu.json’;
$.getJSON(url, function(data) {
window.localStorage.setItem(‘choriziosMenu’, JSON.stringify(data));
showFoodMenuData();
});
}

function showFoodMenuData() {
var data = JSON.parse(window.localStorage.getItem(‘choriziosMenu’));
var images = “”;
$.each(data.menu, function() {
images += ‘<li class=”list-group-item”><img style=”width: 100%;” src= “‘ + this.url + ‘”></li>’;
images += ‘<li class=”list-group-item”>’ + this.description + ‘</li>’;%0

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@lonewiAug 08.2016 — a small solution is to add checking, is there the data in storage, before the request call:

[CODE]

function getFoodMenuData() {
var data = window.localStorage.getItem('choriziosMenu');
if (data) {
return showFoodMenuData();
}
var url = 'http://localhost:8888/Tom_Carp_Final_Project/Chorizios/foodMenu.json';
$.getJSON(url, function(data) {
window.localStorage.setItem('choriziosMenu', JSON.stringify(data));
showFoodMenuData();
});
}

[/CODE]
Copy linkTweet thisAlerts:
@jerseydrummerauthorAug 09.2016 — thank you very much! this solved my problem
×

Success!

Help @jerseydrummer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...