求一个js,页面有一个计时器**时**分**秒
然后不停的计时,当用户鼠标再某处停放了十分钟还不移动的时候停止计时,鼠标再动才继续计时
当用户将窗口最小化也停止计时,当用户不在当前窗口也停止计时
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html140/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> <form name=MyForm id=MyForm> <div style="border:1px solid red" onmouseover="Stop()" onmouseout="Start()"> 放在上面,停止计时 </div> <div id="div1"> </div> </form> <script language="JavaScript" type="text/javascript"> var id; var count =0; window.onload=function(){ id = window.setInterval("Do()",1000); } function Do() { document.getElementById("div1").innerHTML = count; count++; } function Stop() { window.clearInterval(id); } function Start() { id = window.setInterval("Do()",1000); } </script> </body> </html>
窗体最大化和最小化没有办法判断
var intervalId = 0; //计时器 var xy = ""; $(document).ready(function () { $(document).mousemove(function (e) { xy = e.screenX + ":" + e.screenY; }); intervalId=window.setInterval("TimeGo()", "1000"); var resizeTimer = null; $(window).resize(function() { if($(window).height()>500) { if (intervalId) clearInterval(intervalId); intervalId=window.setInterval("TimeGo()", "1000"); } else { if (intervalId) clearInterval(intervalId); } }); // getCommentList(); // setInterval("getCommentList()", "10000"); }); function isMinStatus() { var isMin = false; if (window.outerWidth != undefined) { isMin = window.outerWidth <= 160 && window.outerHeight <= 27; } else { isMin = window.screenTop < -30000 && window.screenLeft < -30000; } return isMin; } var ss=0, mm=0, hh=0; //申明秒、分、时变量并赋初始值 var studyTime=0; var locations = ""; var Pre_Time = 10; var time = 0; var _time = Pre_Time; function TimeGo() { if(isMinStatus()) { } else { if (locations == xy) { time++; _time--; if (time > Pre_Time) { // alert("离开时间:" + time); // clearInterval(intervalId); } else { TimeAdd(); } } else{ if (time > Pre_Time) { alert("离开时间:" + time+"秒"); } time = 0; locations = xy; _time = Pre_Time; TimeAdd(); } } } function TimeAdd() { ss++; //秒值以1为单位递增 if(ss>=60) { //当秒数大于等于60 mm+=1; //分钟加1 ss=0; //秒数退回0 } if(mm>=60) { //当分钟大于等于60 hh+=1; //小时数加1 mm=0; //分钟数退回0 } ss_str = (ss < 10 ? "0" + ss : ss); //格式化秒数 mm_str = (mm < 10 ? "0" + mm : mm); //格式化分钟数 tMsg = "在线学习: " + hh + "小时" + mm_str + "分" + ss_str + "秒"; //输出的字串 document.getElementById("stime").innerHTML = tMsg; //在文档中id号为stime的容器中输出结果 // setTimeout( "TimeGo()",1000); //每一秒钟执行一次函数自身 }
勉强完成的代码是这样的,汗,好多都是百度拼凑出来的
@cx199055: 这个代码 是有效的么?