在网页标题栏中显示计时器

时间:2011-03-19 20:31:19

标签: javascript asp.net webpage

如何使用javascript或任何其他方法在网页的标题栏中显示计时器?

2 个答案:

答案 0 :(得分:4)

<!-- STEP ONE: Paste this code into the HEAD of your HTML document-->

<head>
<script language="javascript" type="text/javascript">
function clock() {
var date = new Date()
var year = date.getYear()
var month = date.getMonth()
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
var months = new Array("", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC")
var monthname = months[month]
if (hour > 12) {hour = hour - 12}
if (minute < 10) {minute = "0" + minute}
if (second < 10) {second = "0" + second}
document.title = monthname + " " + day + ", " + year + " - " + hour + ":" + minute + ":" + second
setTimeout(clock, 1000)
}
</script>
</head>

<!-- STEP TWO: Insert the onLoad event handler into your BODY tag  -->

<body onLoad="clock()">

答案 1 :(得分:0)

我在这里寻找另一种计时器 - 即以秒为单位的当前时间计数器。这是您可以注入控制台的javascript代码:

(function () {
    var start = new Date();
    var pad = function (i) {
        var s = ('0' + Math.floor(i));
        return s.substr(s.length - 2)
    };
    var newTimerStr = function () {
        var t = (new Date() - start)/1000;
        var h = t / 3600;
        var m = (t / 60) % 60;
        var s = (t % 60);
        return [h, m, s].map(pad).join(':');
    };
    setInterval(function () {
        document.title = newTimerStr();
    }, 1000);
}())

或者更好的是,这是书签:

javascript:(function () {var start = new Date(); var pad = function (i) {var s = ('0' + Math.floor(i)); return s.substr(s.length - 2) }; var newTimerStr = function () {var t = (new Date() - start)/1000; var h = t / 3600; var m = (t / 60) % 60; var s = (t % 60); return [h, m, s].map(pad).join(':'); }; setInterval(function () {document.title = newTimerStr(); }, 1000); }());

您可以将其复制为书签。