全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 6706|回复: 10

求分析js代码

[复制链接]
发表于 2013-5-7 13:09:21 | 显示全部楼层 |阅读模式
100金钱
是个关于倒计时的js,倒计时结束在 2013年6月22日24点。。跪求。
这是代码:
  1. /* http://keith-wood.name/countdown.html
  2.    Countdown for jQuery v1.5.4.
  3.    Written by Keith Wood (kbwood{at}iinet.com.au) January 2008.
  4.    Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
  5.    MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
  6.    Please attribute the author if you use it. */

  7. /* Display a countdown timer.
  8.    Attach it with options like:
  9.    $('div selector').countdown(
  10.        {until: new Date(2009, 1 - 1, 1, 0, 0, 0), onExpiry: happyNewYear}); */

  11. (function($) { // Hide scope, no $ conflict

  12. /* Countdown manager. */
  13. function Countdown() {
  14.         this.regional = []; // Available regional settings, indexed by language code
  15.         this.regional[''] = { // Default regional settings
  16.                 // The display texts for the counters
  17.                 labels: ['Years', 'Months', 'Weeks', 'Days', 'Hours', 'Minutes', 'Seconds'],
  18.                 // The display texts for the counters if only one
  19.                 labels1: ['Year', 'Month', 'Week', 'Day', 'Hour', 'Minute', 'Second'],
  20.                 compactLabels: ['y', 'm', 'w', 'd'], // The compact texts for the counters
  21.                 timeSeparator: ':', // Separator for time periods
  22.                 isRTL: false // True for right-to-left languages, false for left-to-right
  23.         };
  24.         this._defaults = {
  25.                 until: 2013.05-07, // new Date(year, mth - 1, day, hr, min, sec) - date/time to count down to
  26.                         // or numeric for seconds offset, or string for unit offset(s):
  27.                         // 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds
  28.                 since: 2013.06-22, // new Date(year, mth - 1, day, hr, min, sec) - date/time to count up from
  29.                         // or numeric for seconds offset, or string for unit offset(s):
  30.                         // 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds
  31.                 timezone: +8, // The timezone (hours or minutes from GMT) for the target times,
  32.                         // or null for client local
  33.                 serverSync: 0, // A function to retrieve the current server time for synchronisation
  34.                 format: "Y", // Format for display - upper case for always, lower case only if non-zero,
  35.                         // 'Y'years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds
  36.                 layout: '', // Build your own layout for the countdown
  37.                 compact: false, // True to display in a compact format, false for an expanded one
  38.                 description: '', // The description displayed for the countdown
  39.                 expiryUrl: '', // A URL to load upon expiry, replacing the current page
  40.                 expiryText: '', // Text to display upon expiry, replacing the countdown
  41.                 alwaysExpire: false, // True to trigger onExpiry even if never counted down
  42.                 onExpiry: null, // Callback when the countdown expires -
  43.                         // receives no parameters and 'this' is the containing division
  44.                 onTick: null // Callback when the countdown is updated -
  45.                         // receives int[7] being the breakdown by period (based on format)
  46.                         // and 'this' is the containing division
  47.         };
  48.         $.extend(this._defaults, this.regional['']);
  49. }

  50. var PROP_NAME = 'countdown';

  51. var Y = 0; // Years
  52. var O = 1; // Months
  53. var W = 2; // Weeks
  54. var D = 3; // Days
  55. var H = 4; // Hours
  56. var M = 5; // Minutes
  57. var S = 6; // Seconds

  58. $.extend(Countdown.prototype, {
  59.         /* Class name added to elements to indicate already configured with countdown. */
  60.         markerClassName: 'hasCountdown',
  61.        
  62.         /* Shared timer for all countdowns. */
  63.         _timer: setInterval(function() { $.countdown._updateTargets(); }, 980),
  64.         /* List of currently active countdown targets. */
  65.         _timerTargets: [],
  66.        
  67.         /* Override the default settings for all instances of the countdown widget.
  68.            @param  options  (object) the new settings to use as defaults */
  69.         setDefaults: function(options) {
  70.                 this._resetExtraLabels(this._defaults, options);
  71.                 extendRemove(this._defaults, options || {});
  72.         },

  73.         /* Convert a date/time to UTC.
  74.            @param  tz     (number) the hour or minute offset from GMT, e.g. +9, -360
  75.            @param  year   (Date) the date/time in that timezone or
  76.                           (number) the year in that timezone
  77.            @param  month  (number, optional) the month (0 - 11) (omit if year is a Date)
  78.            @param  day    (number, optional) the day (omit if year is a Date)
  79.            @param  hours  (number, optional) the hour (omit if year is a Date)
  80.            @param  mins   (number, optional) the minute (omit if year is a Date)
  81.            @param  secs   (number, optional) the second (omit if year is a Date)
  82.            @param  ms     (number, optional) the millisecond (omit if year is a Date)
  83.            @return  (Date) the equivalent UTC date/time */
  84.         UTCDate: function(tz, year, month, day, hours, mins, secs, ms) {
  85.                 if (typeof year == 'object' && year.constructor == Date) {
  86.                         ms = year.getMilliseconds();
  87.                         secs = year.getSeconds();
  88.                         mins = year.getMinutes();
  89.                         hours = year.getHours();
  90.                         day = year.getDate();
  91.                         month = year.getMonth();
  92.                         year = year.getFullYear();
  93.                 }
  94.                 var d = new Date();
  95.                 d.setUTCFullYear(year);
  96.                 d.setUTCDate(1);
  97.                 d.setUTCMonth(month || 0);
  98.                 d.setUTCDate(day || 1);
  99.                 d.setUTCHours(hours || 0);
  100.                 d.setUTCMinutes((mins || 0) - (Math.abs(tz) < 30 ? tz * 60 : tz));
  101.                 d.setUTCSeconds(secs || 0);
  102.                 d.setUTCMilliseconds(ms || 0);
  103.                 return d;
  104.         },

  105.         /* Attach the countdown widget to a div.
  106.            @param  target   (element) the containing division
  107.            @param  options  (object) the initial settings for the countdown */
  108.         _attachCountdown: function(target, options) {
  109.                 var $target = $(target);
  110.                 if ($target.hasClass(this.markerClassName)) {
  111.                         return;
  112.                 }
  113.                 $target.addClass(this.markerClassName);
  114.                 var inst = {options: $.extend({}, options),
  115.                         _periods: [0, 0, 0, 0, 0, 0, 0]};
  116.                 $.data(target, PROP_NAME, inst);
  117.                 this._changeCountdown(target);
  118.         },

  119.         /* Add a target to the list of active ones.
  120.            @param  target  (element) the countdown target */
  121.         _addTarget: function(target) {
  122.                 if (!this._hasTarget(target)) {
  123.                         this._timerTargets.push(target);
  124.                 }
  125.         },

  126.         /* See if a target is in the list of active ones.
  127.            @param  target  (element) the countdown target
  128.            @return  (boolean) true if present, false if not */
  129.         _hasTarget: function(target) {
  130.                 return ($.inArray(target, this._timerTargets) > -1);
  131.         },

  132.         /* Remove a target from the list of active ones.
  133.            @param  target  (element) the countdown target */
  134.         _removeTarget: function(target) {
  135.                 this._timerTargets = $.map(this._timerTargets,
  136.                         function(value) { return (value == target ? null : value); }); // delete entry
  137.         },

  138.         /* Update each active timer target. */
  139.         _updateTargets: function() {
  140.                 for (var i = 0; i < this._timerTargets.length; i++) {
  141.                         this._updateCountdown(this._timerTargets[i]);
  142.                 }
  143.         },

  144.         /* Redisplay the countdown with an updated display.
  145.            @param  target  (jQuery) the containing division
  146.            @param  inst    (object) the current settings for this instance */
  147.         _updateCountdown: function(target, inst) {
  148.                 var $target = $(target);
  149.                 inst = inst || $.data(target, PROP_NAME);
  150.                 if (!inst) {
  151.                         return;
  152.                 }
  153.                 $target.html(this._generateHTML(inst));
  154.                 $target[(this._get(inst, 'isRTL') ? 'add' : 'remove') + 'Class']('countdown_rtl');
  155.                 var onTick = this._get(inst, 'onTick');
  156.                 if (onTick) {
  157.                         onTick.apply(target, [inst._hold != 'lap' ? inst._periods :
  158.                                 this._calculatePeriods(inst, inst._show, new Date())]);
  159.                 }
  160.                 var expired = inst._hold != 'pause' &&
  161.                         (inst._since ? inst._now.getTime() <= inst._since.getTime() :
  162.                         inst._now.getTime() >= inst._until.getTime());
  163.                 if (expired && !inst._expiring) {
  164.                         inst._expiring = true;
  165.                         if (this._hasTarget(target) || this._get(inst, 'alwaysExpire')) {
  166.                                 this._removeTarget(target);
  167.                                 var onExpiry = this._get(inst, 'onExpiry');
  168.                                 if (onExpiry) {
  169.                                         onExpiry.apply(target, []);
  170.                                 }
  171.                                 var expiryText = this._get(inst, 'expiryText');
  172.                                 if (expiryText) {
  173.                                         var layout = this._get(inst, 'layout');
  174.                                         inst.options.layout = expiryText;
  175.                                         this._updateCountdown(target, inst);
  176.                                         inst.options.layout = layout;
  177.                                 }
  178.                                 var expiryUrl = this._get(inst, 'expiryUrl');
  179.                                 if (expiryUrl) {
  180.                                         window.location = expiryUrl;
  181.                                 }
  182.                         }
  183.                         inst._expiring = false;
  184.                 }
  185.                 else if (inst._hold == 'pause') {
  186.                         this._removeTarget(target);
  187.                 }
  188.                 $.data(target, PROP_NAME, inst);
  189.         },

  190.         /* Reconfigure the settings for a countdown div.
  191.            @param  target   (element) the containing division
  192.            @param  options  (object) the new settings for the countdown or
  193.                             (string) an individual property name
  194.            @param  value    (any) the individual property value
  195.                             (omit if options is an object) */
  196.         _changeCountdown: function(target, options, value) {
  197.                 options = options || {};
  198.                 if (typeof options == 'string') {
  199.                         var name = options;
  200.                         options = {};
  201.                         options[name] = value;
  202.                 }
  203.                 var inst = $.data(target, PROP_NAME);
  204.                 if (inst) {
  205.                         this._resetExtraLabels(inst.options, options);
  206.                         extendRemove(inst.options, options);
  207.                         this._adjustSettings(target, inst);
  208.                         $.data(target, PROP_NAME, inst);
  209.                         var now = new Date();
  210.                         if ((inst._since && inst._since < now) ||
  211.                                         (inst._until && inst._until > now)) {
  212.                                 this._addTarget(target);
  213.                         }
  214.                         this._updateCountdown(target, inst);
  215.                 }
  216.         },

  217.         /* Reset any extra labelsn and compactLabelsn entries if changing labels.
  218.            @param  base     (object) the options to be updated
  219.            @param  options  (object) the new option values */
  220.         _resetExtraLabels: function(base, options) {
  221.                 var changingLabels = false;
  222.                 for (var n in options) {
  223.                         if (n.match(/[Ll]abels/)) {
  224.                                 changingLabels = true;
  225.                                 break;
  226.                         }
  227.                 }
  228.                 if (changingLabels) {
  229.                         for (var n in base) { // Remove custom numbered labels
  230.                                 if (n.match(/[Ll]abels[0-9]/)) {
  231.                                         base[n] = null;
  232.                                 }
  233.                         }
  234.                 }
  235.         },
  236.        
  237.         /* Calculate interal settings for an instance.
  238.            @param  target  (element) the containing division
  239.            @param  inst    (object) the current settings for this instance */
  240.         _adjustSettings: function(target, inst) {
  241.                 var serverSync = this._get(inst, 'serverSync');
  242.                 serverSync = (serverSync ? serverSync.apply(target, []) : null);
  243.                 var now = new Date();
  244.                 var timezone = this._get(inst, 'timezone');
  245.                 timezone = (timezone == null ? -now.getTimezoneOffset() : timezone);
  246.                 inst._since = this._get(inst, 'since');
  247.                 if (inst._since) {
  248.                         inst._since = this.UTCDate(timezone, this._determineTime(inst._since, null));
  249.                         if (inst._since && serverSync) {
  250.                                 inst._since.setMilliseconds(inst._since.getMilliseconds() +
  251.                                         now.getTime() - serverSync.getTime());
  252.                         }
  253.                 }
  254.                 inst._until = this.UTCDate(timezone, this._determineTime(this._get(inst, 'until'), now));
  255.                 if (serverSync) {
  256.                         inst._until.setMilliseconds(inst._until.getMilliseconds() +
  257.                                 now.getTime() - serverSync.getTime());
  258.                 }
  259.                 inst._show = this._determineShow(inst);
  260.         },

  261.         /* Remove the countdown widget from a div.
  262.            @param  target  (element) the containing division */
  263.         _destroyCountdown: function(target) {
  264.                 var $target = $(target);
  265.                 if (!$target.hasClass(this.markerClassName)) {
  266.                         return;
  267.                 }
  268.                 this._removeTarget(target);
  269.                 $target.removeClass(this.markerClassName).empty();
  270.                 $.removeData(target, PROP_NAME);
  271.         },

  272.         /* Pause a countdown widget at the current time.
  273.            Stop it running but remember and display the current time.
  274.            @param  target  (element) the containing division */
  275.         _pauseCountdown: function(target) {
  276.                 this._hold(target, 'pause');
  277.         },

  278.         /* Pause a countdown widget at the current time.
  279.            Stop the display but keep the countdown running.
  280.            @param  target  (element) the containing division */
  281.         _lapCountdown: function(target) {
  282.                 this._hold(target, 'lap');
  283.         },

  284.         /* Resume a paused countdown widget.
  285.            @param  target  (element) the containing division */
  286.         _resumeCountdown: function(target) {
  287.                 this._hold(target, null);
  288.         },

  289.         /* Pause or resume a countdown widget.
  290.            @param  target  (element) the containing division
  291.            @param  hold    (string) the new hold setting */
  292.         _hold: function(target, hold) {
  293.                 var inst = $.data(target, PROP_NAME);
  294.                 if (inst) {
  295.                         if (inst._hold == 'pause' && !hold) {
  296.                                 inst._periods = inst._savePeriods;
  297.                                 var sign = (inst._since ? '-' : '+');
  298.                                 inst[inst._since ? '_since' : '_until'] =
  299.                                         this._determineTime(sign + inst._periods[0] + 'y' +
  300.                                                 sign + inst._periods[1] + 'o' + sign + inst._periods[2] + 'w' +
  301.                                                 sign + inst._periods[3] + 'd' + sign + inst._periods[4] + 'h' +
  302.                                                 sign + inst._periods[5] + 'm' + sign + inst._periods[6] + 's');
  303.                                 this._addTarget(target);
  304.                         }
  305.                         inst._hold = hold;
  306.                         inst._savePeriods = (hold == 'pause' ? inst._periods : null);
  307.                         $.data(target, PROP_NAME, inst);
  308.                         this._updateCountdown(target, inst);
  309.                 }
  310.         },

  311.         /* Return the current time periods.
  312.            @param  target  (element) the containing division
  313.            @return  (number[7]) the current periods for the countdown */
  314.         _getTimesCountdown: function(target) {
  315.                 var inst = $.data(target, PROP_NAME);
  316.                 return (!inst ? null : (!inst._hold ? inst._periods :
  317.                         this._calculatePeriods(inst, inst._show, new Date())));
  318.         },

  319.         /* Get a setting value, defaulting if necessary.
  320.            @param  inst  (object) the current settings for this instance
  321.            @param  name  (string) the name of the required setting
  322.            @return  (any) the setting's value or a default if not overridden */
  323.         _get: function(inst, name) {
  324.                 return (inst.options[name] != null ?
  325.                         inst.options[name] : $.countdown._defaults[name]);
  326.         },

  327.         /* A time may be specified as an exact value or a relative one.
  328.            @param  setting      (string or number or Date) - the date/time value
  329.                                 as a relative or absolute value
  330.            @param  defaultTime  (Date) the date/time to use if no other is supplied
  331.            @return  (Date) the corresponding date/time */
  332.         _determineTime: function(setting, defaultTime) {
  333.                 var offsetNumeric = function(offset) { // e.g. +300, -2
  334.                         var time = new Date();
  335.                         time.setTime(time.getTime() + offset * 1000);
  336.                         return time;
  337.                 };
  338.                 var offsetString = function(offset) { // e.g. '+2d', '-4w', '+3h +30m'
  339.                         offset = offset.toLowerCase();
  340.                         var time = new Date();
  341.                         var year = time.getFullYear();
  342.                         var month = time.getMonth();
  343.                         var day = time.getDate();
  344.                         var hour = time.getHours();
  345.                         var minute = time.getMinutes();
  346.                         var second = time.getSeconds();
  347.                         var pattern = /([+-]?[0-9]+)\s*(s|m|h|d|w|o|y)?/g;
  348.                         var matches = pattern.exec(offset);
  349.                         while (matches) {
  350.                                 switch (matches[2] || 's') {
  351.                                         case 's': second += parseInt(matches[1], 10); break;
  352.                                         case 'm': minute += parseInt(matches[1], 10); break;
  353.                                         case 'h': hour += parseInt(matches[1], 10); break;
  354.                                         case 'd': day += parseInt(matches[1], 10); break;
  355.                                         case 'w': day += parseInt(matches[1], 10) * 7; break;
  356.                                         case 'o':
  357.                                                 month += parseInt(matches[1], 10);
  358.                                                 day = Math.min(day, $.countdown._getDaysInMonth(year, month));
  359.                                                 break;
  360.                                         case 'y':
  361.                                                 year += parseInt(matches[1], 10);
  362.                                                 day = Math.min(day, $.countdown._getDaysInMonth(year, month));
  363.                                                 break;
  364.                                 }
  365.                                 matches = pattern.exec(offset);
  366.                         }
  367.                         return new Date(year, month, day, hour, minute, second, 0);
  368.                 };
  369.                 var time = (setting == null ? defaultTime :
  370.                         (typeof setting == 'string' ? offsetString(setting) :
  371.                         (typeof setting == 'number' ? offsetNumeric(setting) : setting)));
  372.                 if (time) time.setMilliseconds(0);
  373.                 return time;
  374.         },

  375.         /* Determine the number of days in a month.
  376.            @param  year   (number) the year
  377.            @param  month  (number) the month
  378.            @return  (number) the days in that month */
  379.         _getDaysInMonth: function(year, month) {
  380.                 return 32 - new Date(year, month, 32).getDate();
  381.         },

  382.         /* Generate the HTML to display the countdown widget.
  383.            @param  inst  (object) the current settings for this instance
  384.            @return  (string) the new HTML for the countdown display */
  385.         _generateHTML: function(inst) {
  386.                 // Determine what to show
  387.                 inst._periods = periods = (inst._hold ? inst._periods :
  388.                         this._calculatePeriods(inst, inst._show, new Date()));
  389.                 // Show all 'asNeeded' after first non-zero value
  390.                 var shownNonZero = false;
  391.                 var showCount = 0;
  392.                 for (var period = 0; period < inst._show.length; period++) {
  393.                         shownNonZero |= (inst._show[period] == '?' && periods[period] > 0);
  394.                         inst._show[period] = (inst._show[period] == '?' && !shownNonZero ? null : inst._show[period]);
  395.                         showCount += (inst._show[period] ? 1 : 0);
  396.                 }
  397.                 var compact = this._get(inst, 'compact');
  398.                 var layout = this._get(inst, 'layout');
  399.                 var labels = (compact ? this._get(inst, 'compactLabels') : this._get(inst, 'labels'));
  400.                 var timeSeparator = this._get(inst, 'timeSeparator');
  401.                 var description = this._get(inst, 'description') || '';
  402.                 var showCompact = function(period) {
  403.                         var labelsNum = $.countdown._get(inst, 'compactLabels' + periods[period]);
  404.                         return (inst._show[period] ? periods[period] +
  405.                                 (labelsNum ? labelsNum[period] : labels[period]) + ' ' : '');
  406.                 };
  407.                 var showFull = function(period) {
  408.                         var labelsNum = $.countdown._get(inst, 'labels' + periods[period]);
  409.                         return (inst._show[period] ?
  410.                                 '<span class="countdown_section"><span class="countdown_amount">' +
  411.                                 periods[period] + '</span><br/>' +
  412.                                 (labelsNum ? labelsNum[period] : labels[period]) + '</span>' : '');
  413.                 };
  414.                 return (layout ? this._buildLayout(inst, layout, compact) :
  415.                         ((compact ? // Compact version
  416.                         '<span class="countdown_row countdown_amount' +
  417.                         (inst._hold ? ' countdown_holding' : '') + '">' +
  418.                         showCompact(Y) + showCompact(O) + showCompact(W) + showCompact(D) +
  419.                         (inst._show[H] ? this._minDigits(periods[H], 2) : '') +
  420.                         (inst._show[M] ? (inst._show[H] ? timeSeparator : '') +
  421.                         this._minDigits(periods[M], 2) : '') +
  422.                         (inst._show[S] ? (inst._show[H] || inst._show[M] ? timeSeparator : '') +
  423.                         this._minDigits(periods[S], 2) : '') :
  424.                         // Full version
  425.                         '<span class="countdown_row countdown_show' + showCount +
  426.                         (inst._hold ? ' countdown_holding' : '') + '">' +
  427.                         showFull(Y) + showFull(O) + showFull(W) + showFull(D) +
  428.                         showFull(H) + showFull(M) + showFull(S)) + '</span>' +
  429.                         (description ? '<span class="countdown_row countdown_descr">' + description + '</span>' : '')));
  430.         },

  431.         /* Construct a custom layout.
  432.            @param  inst     (object) the current settings for this instance
  433.            @param  layout   (string) the customised layout
  434.            @param  compact  (boolean) true if using compact labels
  435.            @return  (string) the custom HTML */
  436.         _buildLayout: function(inst, layout, compact) {
  437.                 var labels = this._get(inst, (compact ? 'compactLabels' : 'labels'));
  438.                 var labelFor = function(index) {
  439.                         return ($.countdown._get(inst,
  440.                                 (compact ? 'compactLabels' : 'labels') + inst._periods[index]) ||
  441.                                 labels)[index];
  442.                 };
  443.                 var digit = function(value, position) {
  444.                         return Math.floor(value / position) % 10;
  445.                 };
  446.                 var subs = {desc: this._get(inst, 'description'), sep: this._get(inst, 'timeSeparator'),
  447.                         yl: labelFor(Y), yn: inst._periods[Y], ynn: this._minDigits(inst._periods[Y], 2),
  448.                         ynnn: this._minDigits(inst._periods[Y], 3), y1: digit(inst._periods[Y], 1),
  449.                         y10: digit(inst._periods[Y], 10), y100: digit(inst._periods[Y], 100),
  450.                         ol: labelFor(O), on: inst._periods[O], onn: this._minDigits(inst._periods[O], 2),
  451.                         onnn: this._minDigits(inst._periods[O], 3), o1: digit(inst._periods[O], 1),
  452.                         o10: digit(inst._periods[O], 10), o100: digit(inst._periods[O], 100),
  453.                         wl: labelFor(W), wn: inst._periods[W], wnn: this._minDigits(inst._periods[W], 2),
  454.                         wnnn: this._minDigits(inst._periods[W], 3), w1: digit(inst._periods[W], 1),
  455.                         w10: digit(inst._periods[W], 10), w100: digit(inst._periods[W], 100),
  456.                         dl: labelFor(D), dn: inst._periods[D], dnn: this._minDigits(inst._periods[D], 2),
  457.                         dnnn: this._minDigits(inst._periods[D], 3), d1: digit(inst._periods[D], 1),
  458.                         d10: digit(inst._periods[D], 10), d100: digit(inst._periods[D], 100),
  459.                         hl: labelFor(H), hn: inst._periods[H], hnn: this._minDigits(inst._periods[H], 2),
  460.                         hnnn: this._minDigits(inst._periods[H], 3), h1: digit(inst._periods[H], 1),
  461.                         h10: digit(inst._periods[H], 10), h100: digit(inst._periods[H], 100),
  462.                         ml: labelFor(M), mn: inst._periods[M], mnn: this._minDigits(inst._periods[M], 2),
  463.                         mnnn: this._minDigits(inst._periods[M], 3), m1: digit(inst._periods[M], 1),
  464.                         m10: digit(inst._periods[M], 10), m100: digit(inst._periods[M], 100),
  465.                         sl: labelFor(S), sn: inst._periods[S], snn: this._minDigits(inst._periods[S], 2),
  466.                         snnn: this._minDigits(inst._periods[S], 3), s1: digit(inst._periods[S], 1),
  467.                         s10: digit(inst._periods[S], 10), s100: digit(inst._periods[S], 100)};
  468.                 var html = layout;
  469.                 // Replace period containers: {p<}...{p>}
  470.                 for (var i = 0; i < 7; i++) {
  471.                         var period = 'yowdhms'.charAt(i);
  472.                         var re = new RegExp('\\{' + period + '<\\}(.*)\\{' + period + '>\\}', 'g');
  473.                         html = html.replace(re, (inst._show[i] ? '$1' : ''));
  474.                 }
  475.                 // Replace period values: {pn}
  476.                 $.each(subs, function(n, v) {
  477.                         var re = new RegExp('\\{' + n + '\\}', 'g');
  478.                         html = html.replace(re, v);
  479.                 });
  480.                 return html;
  481.         },

  482.         /* Ensure a numeric value has at least n digits for display.
  483.            @param  value  (number) the value to display
  484.            @param  len    (number) the minimum length
  485.            @return  (string) the display text */
  486.         _minDigits: function(value, len) {
  487.                 value = '0000000000' + value;
  488.                 return value.substr(value.length - len);
  489.         },

  490.         /* Translate the format into flags for each period.
  491.            @param  inst  (object) the current settings for this instance
  492.            @return  (string[7]) flags indicating which periods are requested (?) or
  493.                     required (!) by year, month, week, day, hour, minute, second */
  494.         _determineShow: function(inst) {
  495.                 var format = this._get(inst, 'format');
  496.                 var show = [];
  497.                 show[Y] = (format.match('y') ? '?' : (format.match('Y') ? '!' : null));
  498.                 show[O] = (format.match('o') ? '?' : (format.match('O') ? '!' : null));
  499.                 show[W] = (format.match('w') ? '?' : (format.match('W') ? '!' : null));
  500.                 show[D] = (format.match('d') ? '?' : (format.match('D') ? '!' : null));
  501.                 show[H] = (format.match('h') ? '?' : (format.match('H') ? '!' : null));
  502.                 show[M] = (format.match('m') ? '?' : (format.match('M') ? '!' : null));
  503.                 show[S] = (format.match('s') ? '?' : (format.match('S') ? '!' : null));
  504.                 return show;
  505.         },
  506.        
  507.         /* Calculate the requested periods between now and the target time.
  508.            @param  inst  (object) the current settings for this instance
  509.            @param  show  (string[7]) flags indicating which periods are requested/required
  510.            @param  now   (Date) the current date and time
  511.            @return  (number[7]) the current time periods (always positive)
  512.                     by year, month, week, day, hour, minute, second */
  513.         _calculatePeriods: function(inst, show, now) {
  514.                 // Find endpoints
  515.                 inst._now = now;
  516.                 inst._now.setMilliseconds(0);
  517.                 var until = new Date(inst._now.getTime());
  518.                 if (inst._since && now.getTime() < inst._since.getTime()) {
  519.                         inst._now = now = until;
  520.                 }
  521.                 else if (inst._since) {
  522.                         now = inst._since;
  523.                 }
  524.                 else {
  525.                         until.setTime(inst._until.getTime());
  526.                         if (now.getTime() > inst._until.getTime()) {
  527.                                 inst._now = now = until;
  528.                         }
  529.                 }
  530.                 // Calculate differences by period
  531.                 var periods = [0, 0, 0, 0, 0, 0, 0];
  532.                 if (show[Y] || show[O]) {
  533.                         // Treat end of months as the same
  534.                         var lastNow = $.countdown._getDaysInMonth(now.getFullYear(), now.getMonth());
  535.                         var lastUntil = $.countdown._getDaysInMonth(until.getFullYear(), until.getMonth());
  536.                         var sameDay = (until.getDate() == now.getDate() ||
  537.                                 (until.getDate() >= Math.min(lastNow, lastUntil) &&
  538.                                 now.getDate() >= Math.min(lastNow, lastUntil)));
  539.                         var getSecs = function(date) {
  540.                                 return (date.getHours() * 60 + date.getMinutes()) * 60 + date.getSeconds();
  541.                         };
  542.                         var months = Math.max(0,
  543.                                 (until.getFullYear() - now.getFullYear()) * 12 + until.getMonth() - now.getMonth() +
  544.                                 ((until.getDate() < now.getDate() && !sameDay) ||
  545.                                 (sameDay && getSecs(until) < getSecs(now)) ? -1 : 0));
  546.                         periods[Y] = (show[Y] ? Math.floor(months / 12) : 0);
  547.                         periods[O] = (show[O] ? months - periods[Y] * 12 : 0);
  548.                         // Adjust for months difference and end of month if necessary
  549.                         var adjustDate = function(date, offset, last) {
  550.                                 var wasLastDay = (date.getDate() == last);
  551.                                 var lastDay = $.countdown._getDaysInMonth(date.getFullYear() + offset * periods[Y],
  552.                                         date.getMonth() + offset * periods[O]);
  553.                                 if (date.getDate() > lastDay) {
  554.                                         date.setDate(lastDay);
  555.                                 }
  556.                                 date.setFullYear(date.getFullYear() + offset * periods[Y]);
  557.                                 date.setMonth(date.getMonth() + offset * periods[O]);
  558.                                 if (wasLastDay) {
  559.                                         date.setDate(lastDay);
  560.                                 }
  561.                                 return date;
  562.                         };
  563.                         if (inst._since) {
  564.                                 until = adjustDate(until, -1, lastUntil);
  565.                         }
  566.                         else {
  567.                                 now = adjustDate(new Date(now.getTime()), +1, lastNow);
  568.                         }
  569.                 }
  570.                 var diff = Math.floor((until.getTime() - now.getTime()) / 1000);
  571.                 var extractPeriod = function(period, numSecs) {
  572.                         periods[period] = (show[period] ? Math.floor(diff / numSecs) : 0);
  573.                         diff -= periods[period] * numSecs;
  574.                 };
  575.                 extractPeriod(W, 604800);
  576.                 extractPeriod(D, 86400);
  577.                 extractPeriod(H, 3600);
  578.                 extractPeriod(M, 60);
  579.                 extractPeriod(S, 1);
  580.                 return periods;
  581.         }
  582. });

  583. /* jQuery extend now ignores nulls!
  584.    @param  target  (object) the object to update
  585.    @param  props   (object) the new settings
  586.    @return  (object) the updated object */
  587. function extendRemove(target, props) {
  588.         $.extend(target, props);
  589.         for (var name in props) {
  590.                 if (props[name] == null) {
  591.                         target[name] = null;
  592.                 }
  593.         }
  594.         return target;
  595. }

  596. /* Process the countdown functionality for a jQuery selection.
  597.    @param  command  (string) the command to run (optional, default 'attach')
  598.    @param  options  (object) the new settings to use for these countdown instances
  599.    @return  (jQuery) for chaining further calls */
  600. $.fn.countdown = function(options) {
  601.         var otherArgs = Array.prototype.slice.call(arguments, 1);
  602.         if (options == 'getTimes') {
  603.                 return $.countdown['_' + options + 'Countdown'].
  604.                         apply($.countdown, [this[0]].concat(otherArgs));
  605.         }
  606.         return this.each(function() {
  607.                 if (typeof options == 'string') {
  608.                         $.countdown['_' + options + 'Countdown'].apply($.countdown, [this].concat(otherArgs));
  609.                 }
  610.                 else {
  611.                         $.countdown._attachCountdown(this, options);
  612.                 }
  613.         });
  614. };

  615. /* Initialise the countdown functionality. */
  616. $.countdown = new Countdown(); // singleton instance

  617. })(jQuery);
复制代码
jquery.countdown.js.zip (7.12 KB, 下载次数: 672)

发表于 2013-5-7 13:10:50 | 显示全部楼层
哥哥 不是有注释么  
发表于 2013-5-7 13:12:08 | 显示全部楼层
看起来需要jQuery,这是个jQuery的倒计时插件,javascript语言的。嗯………………就这些
发表于 2013-5-7 13:21:18 | 显示全部楼层
你想求什么
发表于 2013-5-7 20:42:24 | 显示全部楼层
这有演示
http://jquery-countdown.googlecode.com/svn/trunk/index.html
把代码里的startTime改一下就可以了
发表于 2013-5-7 20:51:35 | 显示全部楼层
是一个倒计时的插件吧,我用过
发表于 2013-5-7 20:52:20 | 显示全部楼层
字多不看  
发表于 2013-5-7 21:43:27 | 显示全部楼层
分析什么?挨句讲解?
发表于 2013-5-7 22:23:50 | 显示全部楼层
看到那么多,直接拉下来了
发表于 2013-5-7 22:25:15 | 显示全部楼层
字多不看  
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-5-27 02:31 , Processed in 0.079299 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表