Home>Article>WeChat Applet> Let’s talk about how to operate local storage synchronously or asynchronously in a mini program
This article will take you to understand the synchronous and asynchronous storage in the WeChat applet, and introduce the methods of synchronous operation of local storage and asynchronous operation of local storage. I hope it will be helpful to everyone!
Unless necessary, try to use the synchronous method, especially for novices, it is recommended to use the synchronous method unless the synchronous method cannot solve the problem Question consider using async method. [Related learning recommendations:Mini Program Development Tutorial]
wx.setStorageSync('key', 'value')
The effect can be seen in the WeChat Mini Program debugger as follows
wx.getStorageSync('key') console.log(wx.getStorageSync('key'))//value
const res = wx.getStorageInfoSync() console.log(res.keys)//["logs", "key"] //res.keys当前 storage 中所有的 key console.log(res.currentSize)//1 //res.currentSize当前占用的空间大小, 单位 KB console.log(res.limitSize)//10240 //res.limitSize限制的空间大小,单位 KB
wx.removeStorageSync('key')
After removal, the storage called key will disappear
wx.clearStorageSync()
Use clearStorageSync as follows Together with the previous logs, they will be cleared.
will The data is stored in the local cache at the specified key. Will overwrite the original content corresponding to the key. Unless the user actively deletes it or it is cleared by the system due to storage space reasons, the data will always be available. The maximum data length allowed to be stored in a single key is 1MB, and the upper limit of all data storage is 10MB.
wx.setStorage({ key:"key2", data:"value2" })
After we store the value, we can see the effect in the debug bar of the WeChat applet. Except for the different operations of access and execution, the results of synchronous and asynchronous are the same. The result of saving and getting the value is the same, except that synchronization is executed sequentially, while asynchronous operation will not cause the interface to stagnate. However, this can almost be ignored, so it is recommended that you use synchronization if it is not necessary.
Removes the specified key from the local cache.
wx.removeStorage({ key: 'key', success (res) { console.log(res) } })
Asynchronously obtain the content of the specified key from the local cache.
wx.getStorage({ key: 'key', success (res) { console.log(res.data) } })
wx.getStorageInfo({ success (res) { console.log(res.keys)//["logs", "key"] //当前 storage 中所有的 key console.log(res.currentSize)//1 //当前占用的空间大小, 单位 KB console.log(res.limitSize)//10240 //限制的空间大小,单位 KB }})
wx.clearStorage()
More programming related knowledge, Please visit:Introduction to Programming! !
The above is the detailed content of Let’s talk about how to operate local storage synchronously or asynchronously in a mini program. For more information, please follow other related articles on the PHP Chinese website!