1、setTimeout() 、setInterval()
setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式。(即n毫秒后执行一次)
setTimeout(code,n)
setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。(即每过n毫秒就执行一次)
setInterval(code,millisec[,"lang"])
setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的参数。
eg: var timer = setInterval(function() {}, 1000);
clearInterval(timer);//函数停止执行
2、获取验证码,倒计时60秒
<input type="button" id="btn" value="免费获取验证码" />
<script type="text/javascript">
var wait=60;
function time(o) {
if (wait == 0) {
o.removeAttribute("disabled");
o.value="免费获取验证码";
wait = 60;
} else {
o.setAttribute("disabled", true);
o.value="重新发送(" + wait + ")";
wait--;
setTimeout(function() {
time(o)
},
1000)
}
}
document.getElementById("btn").onclick=function(){time(this);}