diff --git a/clock.js b/clock.js index 8583194..b48e68c 100755 --- a/clock.js +++ b/clock.js @@ -18,7 +18,7 @@ Drupal.behaviors.clockDisplay = { {long: Drupal.t('February'), short: Drupal.t('Feb')}, {long: Drupal.t('March'), short: Drupal.t('Mar')}, {long: Drupal.t('April'), short: Drupal.t('Apr')}, - // Use context if http://drupal.org/node/488496 gets in. + // Use context if http://drupal.org/node/488496 gets in. {long: Drupal.t('May'), short: Drupal.t('May')}, {long: Drupal.t('June'), short: Drupal.t('Jun')}, {long: Drupal.t('July'), short: Drupal.t('Jul')}, @@ -49,11 +49,6 @@ Drupal.behaviors.clockDisplay = { // Initialize variables. // A PHP date format string. var dateFormat = settings['date_format']; - // Creates a JavaScript date object, from a specially formatted date string. - // Note that due to JavaScript's inferior time zone handling, time zone - // related date formatters will return the time zone of the Drupal site, not - // the visiting user if the time zone is set to 'Local'. - var date = (settings['local'] == 1) ? new Date() : new Date(settings['time']); // Prepare the timezone object. var timeZone = { // The name of the timezone, e.g. 'Europe/London'. @@ -65,6 +60,19 @@ Drupal.behaviors.clockDisplay = { // The name of the offset, e.g. 'GMT'. offsetName: settings['offset_name'], }; + // Creates a JavaScript date object, by calculating the difference between + // the target offset and the current offset and adding that to the current + // time. + // Note that due to JavaScript's inferior time zone handling, time zone + // related date formatters will return the time zone of the Drupal site, not + // the visiting user if the time zone is set to 'Local'. + var date = new Date(); + // JavaScript returns the time zone offset reversely signed as PHP, + // therefore we calculate the difference in the absolute values by adding + // the two numbers. + if (!Drupal.settings['local']) { + date = new Date(date.getTime() + (timeZone.offsetSeconds/60 + date.getTimezoneOffset())*60000); + } clocks[cid] = { date: date, @@ -73,24 +81,24 @@ Drupal.behaviors.clockDisplay = { } } - window.setInterval(function () { - for (var i = 0; i < allSettings['names'].length; i++) { - var cid = allSettings['names'][i]; - clock = clocks[cid]; - $('span.' + cid).each(function () { - // Recalculate the date every second. - - // Add 1 second (1000 milliseconds) to the time. - var timestamp = clock.date.getTime(); - timestamp += 1000; - clock.date = new Date(timestamp); - // Format the clock. - formattedDate = formatDate(clock.date, clock.dateFormat, clock.timeZone, monthNames, weekdayNames); - $('span.' + cid).text(formattedDate); - clocks[cid] = clock; + for (var i = 0; i < allSettings['names'].length; i++) { + var cid = allSettings['names'][i]; + clock = clocks[cid]; + $('span.' + cid).each(function () { + $(this).once(cid, function() { + window.setInterval(function () { + // Add 1 second (1000 milliseconds) to the time. + var timestamp = clock.date.getTime(); + timestamp += 1000; + clock.date = new Date(timestamp); + // Format the clock. + formattedDate = formatDate(clock.date, clock.dateFormat, clock.timeZone, monthNames, weekdayNames); + $('span.' + cid).text(formattedDate); + clocks[cid] = clock; + }, 1000); }); - } - }, 1000); + }); + } } };