Last active
April 19, 2016 19:42
-
-
Save AllThingsSmitty/7606235 to your computer and use it in GitHub Desktop.
Retrieving localStorage from the client
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
'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