Jquery 为什么只显示警报3???

Jquery 为什么只显示警报3???,jquery,html,Jquery,Html,为什么只显示警报3???怎么了?你能解决吗?怎么做 CSS: #foo { width: 200px; height: 30px; display: none; background-color: red; } <div id="foo"><div> $('#foo').text('Alert 1').show(0).delay(5000).hide(0); $('#foo').text('Alert 2').show(0).dela

为什么只显示警报3???怎么了?你能解决吗?怎么做

CSS:

#foo {
    width: 200px;
    height: 30px;
    display: none;
    background-color: red;
}
<div id="foo"><div>
$('#foo').text('Alert 1').show(0).delay(5000).hide(0);

$('#foo').text('Alert 2').show(0).delay(5000).hide(0);

$('#foo').text('Alert 3').show(0).delay(5000).hide(0);
HTML:

#foo {
    width: 200px;
    height: 30px;
    display: none;
    background-color: red;
}
<div id="foo"><div>
$('#foo').text('Alert 1').show(0).delay(5000).hide(0);

$('#foo').text('Alert 2').show(0).delay(5000).hide(0);

$('#foo').text('Alert 3').show(0).delay(5000).hide(0);

它实际上会显示“警报1”,然后是“警报2”,然后是“警报3”,但速度太快,你的眼睛看不见,甚至你的显示器也看不见。 这些行是按顺序执行的。在执行第2行之前,代码不会等待第1行及其5000ms延迟结束

PHP会这样做,因为这是一种同步语言

Javascript没有,因为它是异步的:它可以同时做很多事情

为了等待每个警报5000毫秒,您需要设置回调函数:在事件结束后执行的函数

$('foo')
.text(“警报1”)
.show()
.延迟(3000)
.hide(0,showAlert2)
函数showAlert2(){
$(“#foo”)
.text(“警报2”)
.show()
.延迟(3000)
.hide(0,showAlert3)
}
函数showAlert3(){
$(“#foo”)
.text(“警报3”)
.show()
.延迟(3000)
.hide(0)
}
#foo{
宽度:200px;
高度:30px;
显示:无;
背景色:红色;
}


您需要这个吗:?需要!但是我需要将警报放入“每个”循环中。我不知道有多少警报。如何使用callacks实现这一点?嗯,我认为必要的Callbacks不错,但是,如果我不知道有多少警报??我需要一份警报列表。如何使Callbaks变为变量??