怎么让异步先执行完了再执行js方法

-一个大坑 2018-07-11 10:57:46

function GetView(paymentCode){
GetPaymentView(paymentCode);//调用了一个ajax方法
$("#signedDiv").show();
$("#paymentView").height('auto');
$("#paymentView").parents('.layui-layer').css({
position:'absolute',
top:($(window).height()-$("#paymentView").parents('.layui-layer').outerHeight())/2
});
}

由于ajax异步,导致$("#signedDiv").show();显示div和调整高度都没效果。因为ajax还没返回结果,这个signedDiv还不存在
...全文
2650 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
-一个大坑 2018-07-12
  • 打赏
  • 举报
回复
引用 2 楼 jslang 的回复:
最简单通用的方法是用回调函数,其它的、Promise与Async 或Generator ie浏览器都不支持


function GetPaymentView(paymentCode, callback) {
$.ajax({
url: "......",
success: function (data) {
..............
callback();
}
});
}

function GetView(paymentCode){
GetPaymentView(paymentCode, function () {
$("#signedDiv").show();
$("#paymentView").height('auto');
$("#paymentView").parents('.layui-layer').css({
position:'absolute',
top:($(window).height()-$("#paymentView").parents('.layui-layer').outerHeight())/2
});
});
}

用了这个后,这个页面是正常的,别的页面引用的这个方法有问题了。ajax是异步的,我同事怕处理时还有user乱点,加了遮蔽层。别的引用GetPaymentView的页面,关闭后遮蔽一直在
我现在是在这个页面从新写了GetPaymentView方法

function GetPaymentView(paymentCode, page){
$.post("/DLR008/GetPaymentSignedView?key="+key+"&paymentCode="+paymentCode+"&page="+page,
function(data){
layer.open({
type: 1,
async:false, //在这里加async:false,没效果
title:'費用規則詳情',
skin: 'layui-layer-rim',
area: ['660px', 'auto'],
content: data,
id : "paymentView"
});

})
};
-一个大坑 2018-07-12
  • 打赏
  • 举报
回复

top:($(window).height()-$("#paymentView").parents('.layui-layer').outerHeight())/2

当有滚动条时,弹窗就会跑到上面去了,而不是居中了。
要取什么的高度?屏幕的话浏览器可能没全屏
u010639874 2018-07-12
  • 打赏
  • 举报
回复
写在success里面就可以了
天际的海浪 2018-07-12
  • 打赏
  • 举报
回复
引用 5 楼 happy4944 的回复:
[quote=引用 2 楼 jslang 的回复:]
最简单通用的方法是用回调函数,其它的、Promise与Async 或Generator ie浏览器都不支持


function GetPaymentView(paymentCode, callback) {
$.ajax({
url: "......",
success: function (data) {
..............
callback();
}
});
}

function GetView(paymentCode){
GetPaymentView(paymentCode, function () {
$("#signedDiv").show();
$("#paymentView").height('auto');
$("#paymentView").parents('.layui-layer').css({
position:'absolute',
top:($(window).height()-$("#paymentView").parents('.layui-layer').outerHeight())/2
});
});
}

top:($(window).height()-$("#paymentView").parents('.layui-layer').outerHeight())/2
当有滚动条时,弹窗就会跑到上面去了,而不是居中了[/quote]
position设置为 fixed
-一个大坑 2018-07-12
  • 打赏
  • 举报
回复
引用 2 楼 jslang 的回复:
最简单通用的方法是用回调函数,其它的、Promise与Async 或Generator ie浏览器都不支持


function GetPaymentView(paymentCode, callback) {
$.ajax({
url: "......",
success: function (data) {
..............
callback();
}
});
}

function GetView(paymentCode){
GetPaymentView(paymentCode, function () {
$("#signedDiv").show();
$("#paymentView").height('auto');
$("#paymentView").parents('.layui-layer').css({
position:'absolute',
top:($(window).height()-$("#paymentView").parents('.layui-layer').outerHeight())/2
});
});
}

top:($(window).height()-$("#paymentView").parents('.layui-layer').outerHeight())/2
当有滚动条时,弹窗就会跑到上面去了,而不是居中了
ambit_tsai-微信 2018-07-11
  • 打赏
  • 举报
回复
异步解决方案:回调函数、Promise、Generator、Async。 思路已有,百度去吧
天际的海浪 2018-07-11
  • 打赏
  • 举报
回复
最简单通用的方法是用回调函数,其它的、Promise与Async 或Generator ie浏览器都不支持


function GetPaymentView(paymentCode, callback) {
$.ajax({
url: "......",
success: function (data) {
..............
callback();
}
});
}

function GetView(paymentCode){
GetPaymentView(paymentCode, function () {
$("#signedDiv").show();
$("#paymentView").height('auto');
$("#paymentView").parents('.layui-layer').css({
position:'absolute',
top:($(window).height()-$("#paymentView").parents('.layui-layer').outerHeight())/2
});
});
}

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧