At first I thought of using the storage-event, but as you can see of this question - and this question, and this demonstration at html5demos.com, the use of the storage event is intended to track changes of the localstorage between windows / tabs, not inside the document itself.
But you can create your own event, firing when setItem
is called by overriding setItem :
//create an "onstoragechange" custom event
var storageEvent = document.createEvent('Event');
storageEvent.initEvent('onstoragechanged', true, true);
document.addEventListener('onstoragechanged', function (e) {
alert('value added to localstorage');
//or
alert(localStorage.getItem('test'));
//call the code here, as you above would do after setTimeout
//"callback.call(self);" or whatever
}, false);
//override localStorage.setItem
var oldSetItem = Storage.prototype.setItem;
Storage.prototype.setItem = function() {
oldSetItem.apply(this, arguments);
document.dispatchEvent(storageEvent);
}
//test
localStorage.setItem('test', 'value');
demo / jsfiddle : http://jsfiddle.net/cYLHT/
Now you have an event that is dispatched each time you save anything to localStorage, and the written value actually is present. Expand this with events that helps out your application - like a special event if a certain important key is updated / stored. The above seems maybe as an "off topic" answer, or overkill, but I think it is a far better approach than spreading setTimeouts around the code.