+ 3

Persistence in javascript

I would like to save the state of a game or data from an app from one execution to another. Would localstorage be an option, even if not at SL? How does localStorage work? localStorage.setItem("key",level); level = localStorage.getItem("key"); Ideally, I do not want to use node or frameworks but the simplest solution to enbed in apache cordova build.

24th Jan 2021, 1:26 PM
bell
bell - avatar
11 Respostas
+ 4
bell it's a simple test. Check if this code outputs 'test': localStorage.setItem('x', 'test'); const item = localStorage.getItem('x'); console.log(item); If successful remove the first line and see whether it still prints 'test'. If not the storage isn't persistent and deleted on every run. On my device the first step already fails but it would be interesting whether other devices (with presumably iOS) can use this feature.
24th Jan 2021, 7:12 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 5
For simple data, localstorage should do the job. For larger data with query features, webSQL should be the better option. More information https://www.tutorialspoint.com/html5/html5_web_sql.htm
24th Jan 2021, 1:42 PM
Calviղ
Calviղ - avatar
+ 3
Aaron Eberhardt I confused. Do I need to declare variable x? without the declaration it goes to except.
24th Jan 2021, 7:28 PM
bell
bell - avatar
+ 2
Calviղ i would expect it to be blocked but the chequer I found outputs the opposite. Only I dont know yet how to create a test to check. whether checking for a null value before setting should work. A negative test is never great, which is why I asked, to know if I should keep trying. https://code.sololearn.com/Wao7nBDbuTey/?ref=app
24th Jan 2021, 3:27 PM
bell
bell - avatar
+ 2
Aaron Eberhardt can you please, check my code does not work for you? you should see a cyan square. I keep seeing a navy one (after running it with the line to save this value in storage).
24th Jan 2021, 8:40 PM
bell
bell - avatar
+ 2
i even crashed the app (my ipad cannot display the learning tutotials anymore) and it is still navy! I need to ask a modern ios user how it performs for them. Kode Krasher could you check the code in this thread, please? works on my iphone as well! and in a new copy of the same code
24th Jan 2021, 9:16 PM
bell
bell - avatar
+ 2
thank you so much for your help: Aaron Eberhardt i now know i can use persistance in my codes and that they won't work for most users here.
24th Jan 2021, 9:19 PM
bell
bell - avatar
+ 1
Calviղ thank you for your answer. I have tried localstorage in a sl code but can it work at all in sl? I would just save levels, or maybe an array for a start. where are localstorage data saved to in an app? Thank you for the link to sql.
24th Jan 2021, 2:03 PM
bell
bell - avatar
+ 1
bell i think localstorage feature is disabled by Sololearn app code.
24th Jan 2021, 3:04 PM
Calviղ
Calviղ - avatar
+ 1
bell then I'm speechless as well :D Seems like iOS handles the web view differently. It would be interesting if this works across codes and you can still get the item 'x' when running a different code. Btw. as you might have notices the storage API is a simple key-value storage. This means that you can not directly store objects but you have to store them as JSON and then parse the string when needed.
24th Jan 2021, 7:36 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 1
Yes, I see a cyan square and also it tells me that there is no local storage available.
24th Jan 2021, 8:50 PM
Aaron Eberhardt
Aaron Eberhardt - avatar