Display timer in titlebar of webpage
How I can display timer in titlebar of a webpag开发者_如何转开发e using javascript or any other method?
<!-- 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()">
I was here looking for another kind of timer - i.e. counter from current time in seconds. Here is the javascript code that you can inject into console:
(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);
}())
Or better yet, here is the bookmarklet:
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); }());
Which you can copy this as a bookmarklet.
精彩评论