使用setTimeout来延迟jQuery动作的时间
Using setTimeout to delay timing of jQuery actions
我正在尝试延迟在div中交换文本。它应该像滑动条/旋转轮一样操作文本。
我一定是把代码写错了,因为最后的文本替换从来没有发生过。
另外,我该如何制作动画来引入替换文本(例如,百叶窗)?
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script type="text/javascript">
$(document).ready(function() {
$("#showDiv").click(function() {
$('#theDiv').show(1000, function() {
setTimeout(function() {
$('#theDiv').html('Here is some replacement text', function() {
setTimeout(function() {
$('#theDiv').html('More replacement text goes here');
}, 2500);
});
}, 2500);
});
}); //click function ends
}); //END $(document).ready()
</script>
</head>
<body>
Below me is a DIV called "theDiv".<br><br>
<div id="theDiv" style="background-color:yellow;display:none;width:30%;margin:0 auto;">
This text is inside the Div called "theDiv".
</div><br>
<br>
<input type="button" id="showDiv" value="Show DIV">
</body>
</html>
.html()
只接受字符串或函数作为参数,不能同时接受两者。试试这个:
$("#showDiv").click(function () {
$('#theDiv').show(1000, function () {
setTimeout(function () {
$('#theDiv').html(function () {
setTimeout(function () {
$('#theDiv').html('Here is some replacement text');
}, 0);
setTimeout(function () {
$('#theDiv').html('More replacement text goes here');
}, 2500);
});
}, 2500);
});
}); //click function ends
<<p> jsFiddle例子/strong> 试试这个:
function explode(){
alert("Boom!");
}
setTimeout(explode, 2000);
你也可以使用jQuery的delay()
方法代替setTimeout()
。它会给你更多可读的代码。下面是文档中的一个示例:
$( "#foo" ).slideUp( 300 ).delay( 800 ).fadeIn( 400 );
唯一的限制(我知道)是,它没有给你一个方法来清除超时。如果您需要这样做,那么您最好坚持使用setTimeout
强加给您的所有嵌套回调。
我是这样解决这个问题的鼠标离开后几秒钟菜单关闭(如果悬停没有触发),
//Set timer switch
$setM_swith=0;
$(function(){
$(".navbar-nav li a").click(function(event) {
if (!$(this).parent().hasClass('dropdown'))
$(".navbar-collapse").collapse('hide');
});
$(".navbar-collapse").mouseleave(function(){
$setM_swith=1;
setTimeout(function(){
if($setM_swith==1) {
$(".navbar-collapse").collapse('hide');
$setM_swith=0;}
}, 3000);
});
$(".navbar-collapse").mouseover(function() {
$setM_swith=0;
});
});
相关文章:
- Jquery如何检查今天的时间大于使用给定时间
- Jquery函数在错误的时间提交
- Moment/Jquery-一个简单时间线的愚蠢问题
- javascript/jquery将utc转换为短格式的本地时间
- 正在停止.在jquery中的特定时间间隔内,每次迭代的每次执行
- 用Jquery map和moment js制作一个简单的时间线
- 在jQuery中,短距离长持续时间的精细字体大小动画
- Jquery时间启动计时器,我有一个计时器的代码,但它在页面加载时启动
- Jquery:直到寄存器打开的时间计数器
- 如何检查日期选择器和时间选择器元素是否使用JQuery/Javascript设置
- 当出现多个字段时,jquery时间条目问题
- 如何优化这个jquery循环的执行时间
- 在jquery中对时间数组进行排序
- 使用jquery在特定的时间间隔自动切换/动画css类
- jQuery动画的持续时间就像一个延迟
- jQuery日期时间选择器-如何在javascript中获取日期
- jQuery日期[时间]选择器-如何保存时间范围(+修改日期格式)
- DateTimePicker -禁用一天中已经过去的时间(JQuery)
- 即-按键发生的所有时间- jquery
- 设置日期和时间JQuery的问题