function closecustomBox() {
$('#dialog').hide();
}
$('#dialog').hide();
$('#anotherdialog').hide();
$("#show").click(function() {
$('#dialog').show();
$.when(
setTimeout(function() {
closecustomBox();
}, 3000)
).done(function(x) {
$('#anotherdialog').show();
});
})

#dialog {
width: 101px;
height: 101px;
background-color: red;
}
#anotherdialog {
width: 100px;
height: 100px;
background-color: blue;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dialog"></div>
<div id="anotherdialog"></div>
<div id="show">show</div>
&#13;
我想要发生的是点击show后3秒钟会显示红色框,红色框将隐藏,然后蓝色框应该显示。
我想在这里实现的是不要让2 div一起出现
答案 0 :(得分:2)
如果您想使用$.when
,则需要传入$.Deferred()
作为参数。然后在超时完成后解析$.Deferred()
,这将调用之前传递给.done()
的方法。
我还通过CSS将对话框的初始状态设置为display: none;
,以避免最初需要通过JS隐藏它们。
我还提供了一个不使用jQuery deferred的替代方案,它可以获得相同的结果。
function closecustomBox() {
$('#dialog').hide();
}
var dialog = $('#dialog');
var anotherDialog = $('#anotherdialog');
var timeout;
$("#show").click(function() {
dialog.show();
anotherDialog.hide();
def = $.Deferred();
$.when(def).done(function() {
closecustomBox();
anotherDialog.show();
});
if (timeout) clearTimeout(timeout); // Clear out any old timeouts to avoid flickers and strange behavior
timeout = setTimeout(function() {
def.resolve(); // Resolve the Deferred which will call def.done's callback
}, 3000);
})
// Or if you don't want to use promises you can just elminate them entirely and simplify this example greatly
var timeout2;
$("#show-2").click(function() {
dialog.show();
anotherDialog.hide();
if (timeout) clearTimeout(timeout); // Clear out any old timeouts to avoid flickers and strange behavior
timeout = setTimeout(function() {
closecustomBox();
anotherDialog.show();
}, 3000);
})
#anotherdialog {
width: 100px;
height: 100px;
background-color: blue;
display: none;
}
#dialog {
width: 101px;
height: 101px;
background-color: red;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="dialog"></div>
<div id="anotherdialog"></div>
<div id="show">show</div>
<div id="show-2">Alternate Show</div>
答案 1 :(得分:0)
对代码进行细微更改..
function closecustomBox() {
$('#dialog').hide();
}
$('#dialog').hide();
$('#anotherdialog').hide();
$("#show").click(function() {
$('#dialog').show();
//$.when(
setTimeout(function() {
closecustomBox();
$('#anotherdialog').show();
}, 3000)
/*).done(function(x) {
$('#anotherdialog').show();
});*/
})
&#13;
#dialog {
width: 101px;
height: 101px;
background-color: red;
}
#anotherdialog {
width: 100px;
height: 100px;
background-color: blue;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dialog"></div>
<div id="anotherdialog"></div>
<div id="show">show</div>
&#13;
答案 2 :(得分:0)
$.when
正在获取输入参数,当这些参数解析后,它将执行done
函数。在您的情况下setTimeout
函数成功执行,因此它立即调用done
方法而无需等到3sec
。要使其工作,您需要在此处使用Promise
概念。
function closecustomBox() {
$('#dialog').hide();
}
$('#dialog').hide();
$('#anotherdialog').hide();
$("#show").click(function() {
$('#dialog').show();
var $def = $.Deferred();
$.when(
$def
).done(function(x) {
$('#anotherdialog').show();
});
setTimeout(function() {
closecustomBox();
$def.resolve(true);
}, 3000);
})
#dialog {
width: 101px;
height: 101px;
background-color: red;
}
#anotherdialog {
width: 100px;
height: 100px;
background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dialog"></div>
<div id="anotherdialog"></div>
<div id="show">show</div>