Skip to content

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。

    PropertiesTypeDescription
    isConnectedbooleanIs there a network connection at present?
    networkTypestringNetwork type
  • Legal values

    ValueDescription
    wifiwifi network
    2g2g network
    3g3g network
    4g4g network
    unknownUncommon network types under Android
    noneNo network
  • Sample code:

js
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。

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    networkTypestringNetwork type
  • Legal values

    ValueDescription
    wifiwifi network
    2g2g network
    3g3g network
    4g4g network
    unknownUncommon network types under Android
    noneNo network
  • Sample code:

js
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:

js
const listener = function (res) { console.log(res) }

wx.onNetworkStatusChange(listener)
wx.offNetworkStatusChange(listener) // Pass the same function object used for listening.