1.setInterval(function(){},[time])
示例:这是一个模拟进度条的代码,结束后转向另一页面。
<script type="text/javascript">
$(function () {
var i = 0;
var t = 0;
setInterval(function () {
if (i < 100) {
i = i + 5;
t++;
$("#linepercent").css("height", i + "%");
$("#linetime").text(t + "'");
}
else {
window.location.href = "succ.html";
}
}, 1000);
});
</script>
<style type="text/css">
.progress {
position: absolute;
z-index: 200;
right: 20px;
top: 20px;
}
.line {
width: 13px;
height: 140px;
margin: 0 auto;
background-color: #d6d6d6;
border-radius: 20px;
}
.round {
width: 60px;
line-height: 60px;
background-color: #2bc4c2;
border-radius: 50%;
font-weight: bold;
color: #fff;
margin-top: -5px;
}
.line-now {
background-color: #2bc4c2;
border-radius: 20px;
/*height: 50%;*/
bottom: 0px;
}
.font-nr {
font-size: 15px;
line-height: 45px;
}
</style>
<div id="progressbar" class="progress">
<ul class="line">
<div id="linepercent" class="line-now"></div>
</ul>
<ul id="linetime" class="round font-nr">
0'
</ul>
</div>
2.在某个操作后后面的代码延迟执行
设置一个延时来推迟执行队列中之后的项目。
delay(duration, [queueName])
duration (Integer) 以毫秒为单位的整数,用于设定队列推迟执行的时间。
queueName (String) 可选参数,队列名的字符串。默认是动画队列 fx 。
.delay() 用于将队列中的函数延时执行。它既可以推迟动画队列中函数的执行,也可以用于自定义队列。只有在队列中的连续事件可以被延时,因此不带参数的 .show() 和 .hide() 就不会有延时,因为他们没有使用动画队列。
毫秒为单位的延时,数字越大,动画越慢。字符串 'fast' 和 'slow' 分别代表200和600毫秒的延时。
例如,我们可以在 <div id="foo"> 的 .slideUp() 和 .fadeIn() 动画之间添加800毫秒的延时 :
$('#foo').slideUp(300).delay(800).fadeIn(400);
当这句语句执行后,元素会有300毫秒的卷起动画,接着暂停800毫秒,再出现400毫秒淡入动画。
.delay() 用在jQuery动画或者类似队列中是再好不过的了。但是,由于其本身的限制,比如无法取消延时,所以不应完全用来替代 JavaScript 原生的 setTimeout 函数,后者更适用于通常情况。
3.setTimeout(function(){})
setTimeout()
从载入后延迟指定的时间去执行一个表达式或者是函数;
仅执行一次 ;和window.clearTimeout一起使用.
如果用这个函数,那么第一个进度条的例子可以改成下面的示例代码,
demoProcess();
function demoProcess() {
if (i < 100) {
i = i + 5;
t++;
$("#linepercent").css("height", i + "%");
$("#linetime").text(t + "'");
}
else {
window.location.href = "succ.html";
}
setTimeout(demoProcess, 1000);
}
以上都用了jquery类库。