html52.com
我爱小程序-开发者交流社区

getNetworkType怎么总是在程序最后执行

问题模块
API和组件

代码如下:

bindplay(){

    var that = this

    console.log("0"+getNetWorkType())

    wx.getNetworkType({

      success: function(res) {

        // success

        console.log('1' + res)

        console.log('2' +res.networkType)

        that.setData({networkType:res.networkType}) 

      }, 

      fail(res){

        console.log(res)

      }

    })

    var networkType = that.data.networkType

    console.log('3'+that.data)

    console.log('4'+that.data.networkType) 

    if(that.data.networkType != 'wifi' && !that.data.canPlay){

      that.vedioCtx.pause()

      wx.showModal({

            title: '',

            content: '当前网络为'+that.data.networkType +'是否继续播放',

            showCancel : true,

            success: function(res) {

              if (res.confirm) {

                that.setData({canPlay : true})

                that.vedioCtx.play()

              }

            }

          })

    }


function getNetWorkType(){

  let networkType 

  wx.getNetworkType({

    success: function(res) {

      // success

      networkType = res.networkType

      console.log("-2"+networkType)

    }

  })

  console.log("-1"+networkType)

  return networkType

}


控制台打印结果:

-1undefined

0undefined

3【Obj obj】

4undefined

-2wifi

1【Obj obj】

2wifi

弹出的框的值也是undefined

网友回复

小饼干:

貌似微信方法都是异步的.

SENQ:

异步的

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

真正的个人免签约支付接口

云免签H5支付