function photoCompress(file,w,callback,fjnum){
var ready=new FileReader();
ready.readAsDataURL(file);
ready.onload=function(){
var re=this.result;
alert("222"+this.result)
}
alert("111"+ready.result)
}
代码如上,目前的效果是先执行了alert("111"+ready.result) 在执行alert("222"+this.result), 我想确保先执行alert("222"+this.result),在执行alert("111"+ready.result) 改怎么修改
不是传入了callback,要onload后执行的代码放到callback中
function photoCompress(file,w,callback,fjnum){
var ready=new FileReader();
ready.readAsDataURL(file);
ready.onload=function(){
var re=this.result;
callback(this.result);/////////////////执行回调
alert("222"+this.result)
}
}
///这样调用
photoCompress(file,1200,
function(result){alert("111"+result)}//传入回调
)
这是和异步有关系的,你可以把异步调用用Promise包起来,然后在then中进行alert("111"+xxxx)操作。
或者用Promise包装后用async/await 方式调用,因为这个时候是同步的,可以安装循序执行。