Your code makes no sense. Here is the correct sequence of events:
1. You check if data in local storage exists (usually on start of first layout).
2. If data exists, you load dictionary from local storage.
3. If data is missing, you load some default variables from AJAX.
4. On the end of each level you save the dictionary to local storage.