Network
onNetworkStatusChange
TIP
The API usage is as follows: wx.onNetworkStatusChange(function callback)
Functional description: Listen for network status change events
Parameters and descriptions: function callback, Callback function for network status change events
object.success callback function parameters: object res。
Properties Type Description isConnected boolean Is there a network connection at present? networkType string Network type Legal values
Value Description wifi wifi network 2g 2g network 3g 3g network 4g 4g network unknown Uncommon network types under Android none No network Sample code:
wx.onNetworkStatusChange(function (res) {
console.log(res.isConnected)
console.log(res.networkType)
})getNetworkType
TIP
The API usage is as follows: wx.getNetworkType(Object object)
Functional description: Get network type
Parameters and descriptions: Object object。
Properties Type Default value Required Description success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) object.success callback function parameters: Object res。
Properties Type Description networkType string Network type Legal values
Value Description wifi wifi network 2g 2g network 3g 3g network 4g 4g network unknown Uncommon network types under Android none No network Sample code:
wx.getNetworkType({
success(res) {
const networkType = res.networkType
}
})offNetworkStatusChange
TIP
The API usage is as follows: wx.offNetworkStatusChange(function listener)
Functional description: Remove listener function for network status change events
Parameters and descriptions: function listener,Listener function passed in by onNetworkStatusChange. If this parameter is not passed, all listener functions will be removed
Sample code:
const listener = function (res) { console.log(res) }
wx.onNetworkStatusChange(listener)
wx.offNetworkStatusChange(listener) // Pass the same function object used for listening.