jquery show() 函数延迟
jquery show() function delays
我正在使用 jquery.show()
显示 div,它是 css 中的 "display:none"
。
但是,show()
函数延迟显示 div.
function button_click(){
$("#mydiv").show();
function1();
function2();
}
我试过 $('#mydiv').show({queue:false})
但它不起作用。
#mydiv 仅在 function1 和 function2 完成时显示。
如何使 show()
立即生效?
更新:我发现这是因为我使用了 $.ajxaSetup({async:false})。
我需要发出多个请求,如 $.get 并在所有请求完成后做一些事情,所以我将其设为异步...但是这样 $.show() 函数在所有请求完成之前不起作用...
我不确定你的实际文件,但根据你的代码,这是我的解决方案,使用一个简单的回调函数 one
和 two
不会 运行 直到他们'由 main
函数重新调用,该函数是 hide
方法的回调:
function button_click(){
function one() { console.log('I am one');}
function two() { console.log('I am two');}
$("#mydiv1").hide('slow', function main(){
one();
two();
});
}
button_click();
.child {
color : white;
display : inline-block;
margin : 20px;
min-width : 50px;
min-height : 50px;
padding : 10px;
background-color: olive;
}
.parent {
padding : 20px;
background-color: beige;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="father" class="parent">
<div id="mydiv1" class="child">1</div>
<div id="mydiv2" class="child">2</div>
<div id="mydiv3" class="child">3</div>
<div id="mydiv4" class="child">4</div>
<div id="mydiv5" class="child">5</div>
</div>
希望对您有所帮助
我正在使用 jquery.show()
显示 div,它是 css 中的 "display:none"
。
但是,show()
函数延迟显示 div.
function button_click(){
$("#mydiv").show();
function1();
function2();
}
我试过 $('#mydiv').show({queue:false})
但它不起作用。
#mydiv 仅在 function1 和 function2 完成时显示。
如何使 show()
立即生效?
更新:我发现这是因为我使用了 $.ajxaSetup({async:false})。
我需要发出多个请求,如 $.get 并在所有请求完成后做一些事情,所以我将其设为异步...但是这样 $.show() 函数在所有请求完成之前不起作用...
我不确定你的实际文件,但根据你的代码,这是我的解决方案,使用一个简单的回调函数 one
和 two
不会 运行 直到他们'由 main
函数重新调用,该函数是 hide
方法的回调:
function button_click(){
function one() { console.log('I am one');}
function two() { console.log('I am two');}
$("#mydiv1").hide('slow', function main(){
one();
two();
});
}
button_click();
.child {
color : white;
display : inline-block;
margin : 20px;
min-width : 50px;
min-height : 50px;
padding : 10px;
background-color: olive;
}
.parent {
padding : 20px;
background-color: beige;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="father" class="parent">
<div id="mydiv1" class="child">1</div>
<div id="mydiv2" class="child">2</div>
<div id="mydiv3" class="child">3</div>
<div id="mydiv4" class="child">4</div>
<div id="mydiv5" class="child">5</div>
</div>
希望对您有所帮助