27
7

js实现页面焦点计时

最近需要做一个功能,就是需要记录用户在页面的时候,在焦点的时候,就是说打开了页面看这个页面的时候,打开了转到其他页面就停止计时,转到当前页面才开始…并且转换成00:00的格式..不需要太准确的时候,精确到秒就可以.
为了方便,这里用到了jquery.
关键点:
1. focus和blur事件.
2. setTimeout来计时.
代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<script language="javascript" src="js/jquery-1.3.2.min.js"></script>
<script>
var times=0;//计时
var timer = false;
$(document).ready(function(){
	timedCount();
});
$(window).focus(function(){
	timer = true;
});
$(window).blur( function () {
 	timer = false;
}); 
function timedCount(){
	if(timer){
		times = times+1;
		showTime(times);
	}
	setTimeout('timedCount()',1000);
}
function showTime(tt){
	if(tt>59){
		mm = Math.floor(tt/60);
		if(mm<10){
			mm = "0"+mm;
		}
	}else{
		mm="00";
	}
	ss = tt%60;
	if(ss<10){
		ss = "0"+ss;
	}
	$('#timer').html(mm+":"+ss);
}
</script>
计时:<span id="timer">00:00</span>

相关日志

当前没有评论!

第一个在本文留言。

发表评论

名字(必须)
邮箱(必须),(永不被公布)
网址(建议)

字体为 粗体 是必填项目,邮箱地址 永远不会 公布。

允许部分 HTML 代码:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs(网站链接)必须完整有效 (比如: http://www.vpnall.com),所有标签都必须完整的关闭。

超出部分系统将会自动分段及换行。

请保证评论内容是与日志或 Blog 内容相关的,灌水、攻击性或不恰当的评论 可能 会被编辑或删除。