一、setInterval()/clearInterval()技术学习
都是window对象的方法,可以直接使用。
setInterval(function(){},1000);:每1000毫秒执行一次函数;
clearinterval(item);:清除setInterval()函数的设置,其中item是setInterval()的返回对象,例如:var item = setInterval();
二、页面计时器
html页面中:
<head>
<script type="text/javascript" src="js/common/jquery-2.1.3.js"> </script>
</head>
<body>
<div>
<span class="time"></span>
<span class="stop">stop</span>
</div>
</body>
页面内嵌入js代码:
<script type="text/javascript">
$(function(){
var sec = 0;
var min = 0;
var hour = 0;
var secStr;
var minStr;
var hourStr;
curTime = setInterval(function(){
sec = sec+1;
if(sec > 59){
min = min + 1;
sec = 0;
if(min > 59){
hour = hour + 1;
min = 0;
}
}
secStr = sec < 10 ?"0"+sec:sec;
minStr = min < 10 ?"0"+min:min;
hourStr = hour < 10 ?"0"+hour:hour;
$(".time").html(hourStr+":"+minStr+":"+secStr);
},1000);
$(".stop a").click(function(){
window.clearInterval(curTime);//必须将curTime作为参数传入到该方法中
});
});
</script>