[JavaScript] jQuery智能输出多长时间以前的插件Smart Time Ago →→→→→进入此内容的聊天室

来自 , 2020-05-04, 写在 JavaScript, 查看 103 次.
URL http://www.code666.cn/view/b2ead76d
  1. // Disclaimer: This is not my plugin. I am just posting here for bookmarking purposes.
  2. // Read below
  3.  
  4. // Usage: http://pragmaticly.github.com/smart-time-ago/
  5.  
  6. // Copyright 2012, Terry Tai, Pragmatic.ly
  7. // https://pragmatic.ly/
  8. // Licensed under the MIT license.
  9. // https://github.com/pragmaticly/smart-time-ago/blob/master/LICENSE
  10. //
  11. // Generated by CoffeeScript 1.3.3
  12.  
  13. (function() {
  14.   var TimeAgo;
  15.  
  16.   TimeAgo = (function() {
  17.  
  18.     function TimeAgo(element, options) {
  19.       this.startInterval = 60000;
  20.       this.init(element, options);
  21.     }
  22.  
  23.     TimeAgo.prototype.init = function(element, options) {
  24.       this.$element = $(element);
  25.       this.options = $.extend({}, $.fn.timeago.defaults, options);
  26.       this.updateTime();
  27.       return this.startTimer();
  28.     };
  29.  
  30.     TimeAgo.prototype.startTimer = function() {
  31.       var self;
  32.       self = this;
  33.       return this.interval = setInterval((function() {
  34.         return self.refresh();
  35.       }), this.startInterval);
  36.     };
  37.  
  38.     TimeAgo.prototype.stopTimer = function() {
  39.       return clearInterval(this.interval);
  40.     };
  41.  
  42.     TimeAgo.prototype.restartTimer = function() {
  43.       this.stopTimer();
  44.       return this.startTimer();
  45.     };
  46.  
  47.     TimeAgo.prototype.refresh = function() {
  48.       this.updateTime();
  49.       return this.updateInterval();
  50.     };
  51.  
  52.     TimeAgo.prototype.updateTime = function() {
  53.       var self;
  54.       self = this;
  55.       return this.$element.findAndSelf(this.options.selector).each(function() {
  56.         var timeAgoInWords;
  57.         timeAgoInWords = self.timeAgoInWords($(this).attr(self.options.attr));
  58.         return $(this).html(timeAgoInWords);
  59.       });
  60.     };
  61.  
  62.     TimeAgo.prototype.updateInterval = function() {
  63.       var filter, newestTime, newestTimeInMinutes, newestTimeSrc;
  64.       if (this.$element.findAndSelf(this.options.selector).length > 0) {
  65.         if (this.options.dir === "up") {
  66.           filter = ":first";
  67.         } else if (this.options.dir === "down") {
  68.           filter = ":last";
  69.         }
  70.         newestTimeSrc = this.$element.findAndSelf(this.options.selector).filter(filter).attr(this.options.attr);
  71.         newestTime = this.parse(newestTimeSrc);
  72.         newestTimeInMinutes = this.getTimeDistanceInMinutes(newestTime);
  73.         if (newestTimeInMinutes >= 0 && newestTimeInMinutes <= 44 && this.startInterval !== 60000) {
  74.           this.startInterval = 60000;
  75.           return this.restartTimer();
  76.         } else if (newestTimeInMinutes >= 45 && newestTimeInMinutes <= 89 && this.startInterval !== 60000 * 22) {
  77.           this.startInterval = 60000 * 22;
  78.           return this.restartTimer();
  79.         } else if (newestTimeInMinutes >= 90 && newestTimeInMinutes <= 2519 && this.startInterval !== 60000 * 30) {
  80.           this.startInterval = 60000 * 30;
  81.           return this.restartTimer();
  82.         } else if (newestTimeInMinutes >= 2520 && this.startInterval !== 60000 * 60 * 12) {
  83.           this.startInterval = 60000 * 60 * 12;
  84.           return this.restartTimer();
  85.         }
  86.       }
  87.     };
  88.  
  89.     TimeAgo.prototype.timeAgoInWords = function(timeString) {
  90.       var absolutTime;
  91.       absolutTime = this.parse(timeString);
  92.       return "" + this.options.lang.prefixes.ago + (this.distanceOfTimeInWords(absolutTime)) + this.options.lang.suffix;
  93.     };
  94.  
  95.     TimeAgo.prototype.parse = function(iso8601) {
  96.       var timeStr;
  97.       timeStr = $.trim(iso8601);
  98.       timeStr = timeStr.replace(/\.\d\d\d+/, "");
  99.       timeStr = timeStr.replace(/-/, "/").replace(/-/, "/");
  100.       timeStr = timeStr.replace(/T/, " ").replace(/Z/, " UTC");
  101.       timeStr = timeStr.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2");
  102.       return new Date(timeStr);
  103.     };
  104.  
  105.     TimeAgo.prototype.getTimeDistanceInMinutes = function(absolutTime) {
  106.       var timeDistance;
  107.       timeDistance = new Date().getTime() - absolutTime.getTime();
  108.       return Math.round((Math.abs(timeDistance) / 1000) / 60);
  109.     };
  110.  
  111.     TimeAgo.prototype.distanceOfTimeInWords = function(absolutTime) {
  112.       var dim;
  113.       dim = this.getTimeDistanceInMinutes(absolutTime);
  114.       if (dim === 0) {
  115.         return "" + this.options.lang.prefixes.lt + " " + this.options.lang.units.minute;
  116.       } else if (dim === 1) {
  117.         return "1 " + this.options.lang.units.minute;
  118.       } else if (dim >= 2 && dim <= 44) {
  119.         return "" + dim + " " + this.options.lang.units.minutes;
  120.       } else if (dim >= 45 && dim <= 89) {
  121.         return "" + this.options.lang.prefixes.about + " 1 " + this.options.lang.units.hour;
  122.       } else if (dim >= 90 && dim <= 1439) {
  123.         return "" + this.options.lang.prefixes.about + " " + (Math.round(dim / 60)) + " " + this.options.lang.units.hours;
  124.       } else if (dim >= 1440 && dim <= 2519) {
  125.         return "1 " + this.options.lang.units.day;
  126.       } else if (dim >= 2520 && dim <= 43199) {
  127.         return "" + (Math.round(dim / 1440)) + " " + this.options.lang.units.days;
  128.       } else if (dim >= 43200 && dim <= 86399) {
  129.         return "" + this.options.lang.prefixes.about + " 1 " + this.options.lang.units.month;
  130.       } else if (dim >= 86400 && dim <= 525599) {
  131.         return "" + (Math.round(dim / 43200)) + " " + this.options.lang.units.months;
  132.       } else if (dim >= 525600 && dim <= 655199) {
  133.         return "" + this.options.lang.prefixes.about + " 1 " + this.options.lang.units.year;
  134.       } else if (dim >= 655200 && dim <= 914399) {
  135.         return "" + this.options.lang.prefixes.over + " 1 " + this.options.lang.units.year;
  136.       } else if (dim >= 914400 && dim <= 1051199) {
  137.         return "" + this.options.lang.prefixes.almost + " 2 " + this.options.lang.units.years;
  138.       } else {
  139.         return "" + this.options.lang.prefixes.about + " " + (Math.round(dim / 525600)) + " " + this.options.lang.units.years;
  140.       }
  141.     };
  142.  
  143.     return TimeAgo;
  144.  
  145.   })();
  146.  
  147.   $.fn.timeago = function(options) {
  148.     if (options == null) {
  149.       options = {};
  150.     }
  151.     return this.each(function() {
  152.       var $this, data;
  153.       $this = $(this);
  154.       data = $this.data("timeago");
  155.       if (!data) {
  156.         $this.data("timeago", new TimeAgo(this, options));
  157.       }
  158.       if (typeof options === 'string') {
  159.         return data[options]();
  160.       }
  161.     });
  162.   };
  163.  
  164.   $.fn.findAndSelf = function(selector) {
  165.     return this.find(selector).add(this.filter(selector));
  166.   };
  167.  
  168.   $.fn.timeago.Constructor = TimeAgo;
  169.  
  170.   $.fn.timeago.defaults = {
  171.     selector: 'time.timeago',
  172.     attr: 'datetime',
  173.     dir: 'up',
  174.     lang: {
  175.       units: {
  176.         second: "second",
  177.         seconds: "seconds",
  178.         minute: "minute",
  179.         minutes: "minutes",
  180.         hour: "hour",
  181.         hours: "hours",
  182.         day: "day",
  183.         days: "days",
  184.         month: "month",
  185.         months: "months",
  186.         year: "year",
  187.         years: "years"
  188.       },
  189.       prefixes: {
  190.         lt: "less than a",
  191.         about: "about",
  192.         over: "over",
  193.         almost: "almost",
  194.         ago: ""
  195.       },
  196.       suffix: ' ago'
  197.     }
  198.   };
  199.  
  200. }).call(this);
  201. //javascript/5183

回复 "jQuery智能输出多长时间以前的插件Smart Time Ago "

这儿你可以回复上面这条便签

captcha