87,989
社区成员
发帖
与我相关
我的任务
分享
function Clock()
{
this.showtip = null; //保存页面传进来的元素id;
this.timerID = null;
this.timerRunning = false;
// Make sure the clock is stopped
this.show = function(showtip) //定时循环的函数
{
this.showtip = showtip; //将属性赋值。
alert(typeof this.showtip);
if(this.timerRunning)
clearTimeout(this.timerID);
this.timerRunning = false
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var timeValue = "" + ((hours < 10) ? "0" : "") + hours;
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
if(showtip != null)
{
this.showtip.innerHTML = timeValue;
}
this.timerID = setTimeout("show(this.showtip)" ,1000);
this.timerRunning = true;
}
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script type="text/javascript" src="time.js"></script>
<script type="text/javascript">
document.onreadystatechange = function()
{
if (document.readyState == "complete")
{
var clock = new top.Clock();
clock.show(oClock);
// 文档状态为完成,开始执行相应处理
}
}
</script>
</head>
<body>
<div style=" position: absolute; background-color:#3366cc; width:50%; height:50%;" id = "oDiv">
<span style="position: relative ; color:#FFF; font-family:黑体; font-weight:bold; font-size:15px; top:10%; left:80%;" ID="oClock"></span>
</div>
</body>
</html>
this.showtip = null;
this.timerID = null;
this.timerRunning = false;
// Make sure the clock is stopped
this.show = function()
{
var obj = this;
//alert(typeof obj.showtip);
if(obj.timerRunning)
clearTimeout(obj.timerID);
obj.timerRunning = false;
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var timeValue = "" + ((hours < 10) ? "0" : "") + hours;
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
if(obj.showtip != null)
obj.showtip.innerHTML = timeValue;
obj.timerID = setTimeout(function(){obj.show();},1000);
obj.timerRunning = true;
}