Jump to content

User:Animum/liveclock.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* 
* 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);