Jump to content

User:Davidgothberg/clock.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Davidgothberg (talk | contribs) at 22:33, 18 September 2008 (Moving the DavidClock code here from my "/monobook.js".). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
/***** DavidClock **************************************************

  Puts an UTC clock in the upper right corner of all pages.
  It also has a link to purge the current page. (Click the clock 
  to purge the page.)

  To use this script put this code in your 
  [[User:Yourname/monobook.js]] file:

    importScript( "User:Davidgothberg/clock.js" );

  Default is a static clock that shows when the page was loaded,
  and a clock that ticks once a minute. This can be changed 
  by adding one of the lines below to your "/monobook.js". 
  Don't set below 60 seconds on slow computers. 

  window.davidClock = -60;  //Only ticking clock, every minute.
  window.davidClock = -5;   //Only ticking clock, every 5 seconds.
  window.davidClock = 0;    //Only static clock
  window.davidClock = 5;    //Static + ticking clock, every 5 seconds.
  window.davidClock = 60;   //Static + ticking clock, every minute.
  window.davidClock = 120;  //Static + ticking clock, every 2 minutes.

  Technical details:
  Only thing that runs on each tick is the inner function 
  updateTimeMinutes() or updateTimeSeconds().

  Based on code from [[User:AzaToth]] and [[User:Ilmari Karonen]].
  Maintainer: [[User:Davidgothberg]]
*/

 
/* DavidClock */
addOnloadHook( function() {
 
  var static = true;
  var tick = 60;
 
  /* If the user has not declared "window.davidClock = x;"
     then none of these if-cases become true, and the defaults
     above will be used. */
  if( window.davidClock >= 0 ) {
    tick = window.davidClock;
  }
  else if( window.davidClock < 0 ) {
    static = false;
    tick = 0 - window.davidClock;
  }
 
  /* Optimised ticker for 60 seconds or longer tick intervals. */
  function updateTimeMinutes() {
    var now = new Date();
    var h = now.getUTCHours();
    var m = now.getUTCMinutes();
    linkNode.innerHTML = ( h < 10 ? '0' : '' ) + h + ( m < 10 ? ':0' : ':' ) + m;
    setTimeout( updateTimeMinutes, tick * 1000 );
  };
 
  /* Optimised ticker for 1-59 seconds tick intervals. */
  function updateTimeSeconds() {
    var now = new Date();
    var h = now.getUTCHours();
    var m = now.getUTCMinutes();
    var s = now.getUTCSeconds();
    linkNode.innerHTML = (h<10?'0':'') + h + (m<10?':0':':') + m + (s<10?':0':':') + s;
    setTimeout( updateTimeSeconds, tick * 1000 );
  };
 
  /* Start the ticking clock. */
  if( tick ) {
    var tabNode = addPortletLink( 'p-personal', wgScriptPath + '/index.php?title=' + encodeURIComponent(wgPageName) + '&action=purge', '', 'pt-utcticker' );
    tabNode.style.fontWeight = 'bolder';
    var linkNode = tabNode.getElementsByTagName("a")[0];
 
    if( tick >= 60 ) {
      updateTimeMinutes();
    }
    else {
      updateTimeSeconds();
    }
  }
 
  /* Create the static clock. */
  if( static ) {
    var tabNode2 = addPortletLink( 'p-personal', wgScriptPath + '/index.php?title=' + encodeURIComponent(wgPageName) + '&action=purge', '', 'pt-utcstatic' );
    var linkNode2 = tabNode2.getElementsByTagName("a")[0];
 
    var now = new Date();
    var h = now.getUTCHours();
    var m = now.getUTCMinutes();
    linkNode2.innerHTML = ( h < 10 ? '0' : '' ) + h + ( m < 10 ? ':0' : ':' ) + m;
  }
 
} );  /* End DavidClock */