小程序為了用戶體驗,所有的request均為異步請求,不會阻塞程序運行
百牛信息技術bainiu.ltd整理發布於博客園
所以當你需要同步請求,鎖死操作時,最好將所有的邏輯寫在success:function(){}
里面,
不然后出現返回值為空的尷尬
錯誤代碼示例:

更改后的代碼為:
onShow:function(){// 頁面顯示var commonFunction = require('../../pages/index/common'),that = this;var interval = setInterval(function(){that.setData({nowTime : commonFunction.formatTime(new Date())})},1000);var request = function(latitude,longitude){wx.request({url: that.globalData.API_URL + 'getLocation',data: {latitude : latitude,longitude : longitude},method: 'GET',success: function(res){let result = res.data.data;result = JSON.parse(result);console.log(result);}});};wx.getLocation({"type" : 'gcj02',"success" : function(res){const latitude = res.latitude;const longitude = res.longitude;request(latitude,longitude);},"fail" : function(e){console.log(e);}});},此文作用僅為填坑,
