search
HomeWeChat AppletMini Program DevelopmentLet'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!

Let's talk about how to operate local storage synchronously or asynchronously in a mini program

1. Synchronous operation of local storage

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 Synchronous Storage:

wx.setStorageSync('key', 'value')

The effect can be seen in the WeChat Mini Program debugger as follows

Lets talk about how to operate local storage synchronously or asynchronously in a mini program

wx.getStorageSync synchronously obtains:

wx.getStorageSync('key')
console.log(wx.getStorageSync('key'))//value

wx.getStorageInfoSync() The information in the current storage

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 synchronously removes one:

wx.removeStorageSync('key')

After removal, the storage called key will disappear

Lets talk about how to operate local storage synchronously or asynchronously in a mini program

wx.clearStorageSync clears everything synchronously:

wx.clearStorageSync()

Use clearStorageSync as follows Together with the previous logs, they will be cleared.

Lets talk about how to operate local storage synchronously or asynchronously in a mini program

2. Asynchronous operation of local storage

1.wx.setStorage asynchronous storage value;

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.

Lets talk about how to operate local storage synchronously or asynchronously in a mini program

2.wx.removeStorage() removes the specified value

Removes the specified key from the local cache.

wx.removeStorage({
    key: 'key',
    success (res) {
        console.log(res)
    }
})

3.wx.getStorage(); Get the value

Asynchronously obtain the content of the specified key from the local cache.

wx.getStorage({
  key: 'key',
  success (res) {
     console.log(res.data)
  }
})

4.wx.getStorageInfo Gets the information in the current storage

wx.getStorageInfo({
   success (res) {
   console.log(res.keys)//["logs", "key"]
   //当前 storage 中所有的 key
   console.log(res.currentSize)//1
   //当前占用的空间大小, 单位 KB
   console.log(res.limitSize)//10240
   //限制的空间大小,单位 KB
}})

5.wx.clearStorage(); Clear all keys

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!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool