User:Ilmari Karonen/liveclock.js

From Wikipedia, the free encyclopedia
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.
/* To turn on seconds display, add the following line to your monobook.js:
window.liveClockShowSeconds = true;
*/

$(function () {
  var tabNode = mw.util.addPortletLink(
      "p-personal",
      mw.config.get( 'wgScript' ) + "?title=" + encodeURIComponent( mw.config.get( 'wgPageName' ) ) + "&action=purge",
      "",
      "utcdate"
  );
  var linkNode = tabNode.getElementsByTagName("a")[0];

  var updateTimeSeconds = function () {
    var now = new Date ();
    var h = now.getUTCHours();
    var m = now.getUTCMinutes();
    var s = now.getUTCSeconds() + (now.getUTCMilliseconds() >= 500 ? 1 : 0);
    linkNode.innerHTML = (h<10?"0":"") + h + (m<10?":0":":") + m + (s<10?":0":":") + s;
    setTimeout(updateTimeSeconds, 1500 - ((now.getTime() + 500) % 1000));
  };

  var updateTimeMinutes = function () {
    var now = new Date ();
    var h = now.getUTCHours();
    var m = now.getUTCMinutes() + (now.getUTCSeconds() >= 30 ? 1 : 0);
    linkNode.innerHTML = (h<10?"0":"") + h + (m<10?":0":":") + m;
    setTimeout(updateTimeMinutes, 90000 - ((now.getTime() + 30000) % 60000));
  };

  if (window.liveClockShowSeconds) updateTimeSeconds();
  else updateTimeMinutes();
});