Skip to content

Instantly share code, notes, and snippets.

@AllThingsSmitty
Last active April 19, 2016 19:42
Show Gist options
  • Save AllThingsSmitty/7606235 to your computer and use it in GitHub Desktop.
Save AllThingsSmitty/7606235 to your computer and use it in GitHub Desktop.
Retrieving localStorage from the client
'use strict';
function getStorage(type) {
var storage = window[type + 'Storage'],
delta = 0,
li = document.createElement('li');
if (!window[type + 'Storage']) {
return;
}
if (storage.getItem('value')) {
delta = ((new Date()).getTime() - (new Date()).setTime(storage.getItem('timestamp'))) / 1000;
li.innerHTML = type + 'Storage: ' + storage.getItem('value') + ' (last updated: ' + delta + 's ago)';
} else {
li.innerHTML = type + 'Storage is empty';
}
document.querySelector('#previous').appendChild(li);
}
getStorage('local');
addEvent(document.querySelector('#local'), 'keyup', function () {
localStorage.setItem('value', this.value);
localStorage.setItem('timestamp', (new Date()).getTime());
});
addEvent(document.querySelector('#clear'), 'click', function () {
localStorage.clear();
document.querySelector('#previous').innerHTML = '';
getStorage('local');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment