User:Animum/liveclock.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Animum/liveclock. |
/*
* Modified version of [[User:AzaToth/liveclock.js]]
* This version has a feature that makes it, at most, only a few milliseconds ahead of the actual time.
*
* Yes, a few hundred milliseconds (the normal margin of error for these clocks) doesn't really make that much of a difference,
* but I'm just that anal.
*/
function showTime() {
var dateNode = liveClock.node;
if(!dateNode) {
return;
}
var now = new Date();
var stabilizer = now.getUTCMilliseconds()/10;
var hh = now.getUTCHours();
var mm = now.getUTCMinutes();
var ss = now.getUTCSeconds();
var time = ( hh < 10 ? '0' + hh : hh ) + ':' + ( mm < 10 ? '0' + mm : mm ) + ':' + ( ss < 10 ? '0' + ss : ss ) + ', ' + now.getUTCDate() + ' ' + ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][now.getUTCMonth()] + ' ' + now.getUTCFullYear() + ' (UTC)';
if(dateNode && dateNode.firstChild && dateNode.firstChild.firstChild) dateNode.firstChild.replaceChild(document.createTextNode(time), dateNode.firstChild.firstChild);
if(stabilizer < 1) { //Being under 10 milliseconds ahead is reasonably accurate.
window.setTimeout(showTime, 1000);
} else {
window.setTimeout(showTime, 1000-(stabilizer*10)); //Trying to get the clock as accurate as possible
}
}
function liveClock() {
liveClock.node = mw.util.addPortletLink('p-personal', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=' + mw.config.get('wgPageName') + '&action=purge' , '', 'utcdate');
liveClock.node.style.fontWeight = 'normal';
liveClock.node.style.textTransform = 'none';
showTime();
}
$(liveClock);