getNetworkType怎么总是在程序最后执行
发布于 5 年前 作者 ezheng 2332 次浏览 来自 问答

代码如下:

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

2 回复

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

回到顶部