日期:2014-05-16 浏览次数:20419 次
<html> <head> <title>标题</title> <script src="../js/clock.js" mce_src="js/clock.js" language="JavaScript"> </script> </head> <body> <div id="clock"></div> <script language="JavaScript"> <!-- var clockDiv = document.getElementById("clock"); // 自定义时钟对象,实时显示当前时间 var clickObj = new Clock(clockDiv); /* * setInterval()方法使用,周期性的调用clickObj.getCurrentDate() * 以更新显示内容 */ var intervalId = window.setInterval("clickObj.getCurrentDate()", 1000); // --> </script> <br> <!-- 此处clearInterval()方法的参数intervalId就是上面setInterval()调用后的 返回值 --> <input type="button" onclick="window.clearInterval(intervalId)" value="停止计时" /> </body> </html>
<html> <head> <title>标题</title> <script src="../js/clock.js" mce_src="js/clock.js" language="JavaScript"> </script> </head> <body> <div id="clock"></div> <script language="JavaScript"> <!-- var clockDiv = document.getElementById("clock"); // 自定义时钟对象,实时显示当前时间 var clickObj = new Clock(clockDiv); /* * setInterval()方法使用,周期性的调用clickObj.getCurrentDate() * 以更新显示内容 */ var intervalId = window.setInterval("clickObj.getCurrentDate()", 1000); // --> </script> <br> <!-- 此处clearInterval()方法的参数intervalId就是上面setInterval()调用后的 返回值 --> <input type="button" onclick="window.clearInterval(intervalId)" value="停止计时" /> </body> </html>
/* * param clockDiv * 传入的div对象 */ function Clock(clockDiv) { this.clockDiv = clockDiv; this.getCurrentDate = function() { // 获取当前日期 var currDate = new Date(); // 分别获取 年、月、日、时、分、秒 var currDateTime = currDate.getYear(); currDateTime += "-"; currDateTime += (currDate.getMonth() + 1); currDateTime += "-"; currDateTime += currDate.getDate(); currDateTime += " "; currDateTime += currDate.getHours(); currDateTime += ":"; currDateTime += currDate.getMinutes(); currDateTime += ":"; currDateTime += currDate.getSeconds(); // 将当前时间赋值到div对象中 this.clockDiv.innerHTML = currDateTime; } }