JSFiddle - React, Tailwind, and code Playground

by Cristiano Oliveira

JavaScript

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];
				
			}
		
		}
		
	}

};

window.addEventListener("beforeunload", function () {

	Storage.sync();
alert ("data")
	
}, false);





Storage.set("id", "data");