小程序如何防止网络操作延迟导致无法获取数据

小程序网络请求异步,如何防止网络操作延迟导致无法获取数据?例如官方quikStart项目demo中的有没有简单易用的解决方法?

https://img4.mukewang.com/5c8f4c6d00019c4b06010303.jpg

RISEBY
浏览 1339回答 2
2回答

MYYA

var Callback = function(keys, callback) {&nbsp; &nbsp; keys = [].concat(keys);&nbsp; &nbsp; var value;&nbsp; &nbsp; this.value = function(v) {&nbsp; &nbsp; &nbsp; &nbsp; if (v === undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; value = v;&nbsp; &nbsp; };&nbsp; &nbsp; this.emit = function(key) {&nbsp; &nbsp; &nbsp; &nbsp; var i = keys.indexOf(key);&nbsp; &nbsp; &nbsp; &nbsp; if (i>=0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keys.splice(i, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (keys.length<=0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback(value);&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };};var c= new Callback(['test1', 'test2'], console.log);setTimeout(function() {&nbsp; &nbsp; c.value(1);&nbsp; &nbsp; c.emit('test1');},100);setTimeout(function() {&nbsp; &nbsp; c.emit('test2');},101);

慕的地10843

异步过程,使用回调。getUserInfo的success里面有回调,回调数据是data,只需要将对data的处理放到success回调里面即可。如果对data的处理需要异步,则构造一个dataProduce函数,并设置回调,继而通过回调完成dataProduce。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript