仅在上一个执行完毕后才调用函数

问题描述 投票:0回答:1

我试图在hideDiv运行后调用closeModal,所以有时间看到div被隐藏了,但我正在寻找除setTimeout之外的另一种方式:

function hideDiv () {
   $('.div').hide();
} 
function closeModal() { 
if($('.div').lenght === 0) {
   $.modal.close();
}}

$('.button').on('click', () => {
hideDiv();
setTimeout(function(){ closeModal() }, 1000);
});
javascript function asynchronous callback execution
1个回答
0
投票
function hideDiv () {
   $('.div').hide();
} 
function closeModal() { 
if($('.div').lenght === 0) {
   $.modal.close();
}}

$('.button').on('click',aync () => {
await hideDiv();
closeModal() ;
});

给这个镜头


0
投票

jQuery hide函数具有回调

用法

function hideDiv(callback) {
  return $('.div').hide(400, callback);
}

function closeModal() {
  if ($('.div').lenght === 0) {
    $.modal.close();
  }
}

$('.button').on('click', () => {
  hideDiv(closeModal);
});
.div {
  background: blue;
  width: 50px;
  height: 50px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="div"></div>
<button class="button" type="button">Hide</div>
© www.soinside.com 2019 - 2024. All rights reserved.