代码如下:
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