Last active
January 2, 2018 20:54
-
-
Save louisstow/5610444 to your computer and use it in GitHub Desktop.
A simple storage object in JavaScript using 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 Storage = { | |
_cache: {}, | |
get: function (key) { | |
if (!this._cache[key]) { | |
this._cache[key] = localStorage[key]; | |
} | |
return this._cache[key]; | |
}, | |
set: function (key, value) { | |
this._cache[key] = value; | |
}, | |
sync: function () { | |
for (var key in this._cache) { | |
if (this._cache.hasOwnProperty(key)) { | |
localStorage[key] = this._cache[key]; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Storage.get(key)
- Return the saved item by key.Storage.set(key, value)
- Saved an item by key.Storage.sync()
- Sync all new data tolocalStorage
. Do not use this method during gameplay.