Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactor the time comparisons, avoid float comparisons as possible as…

… I can. And Optimize the updateInterval and distanceOfTimeInWords functions.
  • Loading branch information...
commit 15c47c73bdec372fbb1e48ed63efa2095efd6e92 1 parent 23ab767
Terry poshboytl authored

Showing 2 changed files with 102 additions and 70 deletions. Show diff stats Hide diff stats

  1. +52 28 lib/timeago.js
  2. +50 42 src/timeago.coffee
80 lib/timeago.js
@@ -5,7 +5,7 @@
5 5 TimeAgo = (function() {
6 6
7 7 function TimeAgo(element, options) {
8   - this.startInterval = 30000;
  8 + this.startInterval = 5000;
9 9 this.init(element, options);
10 10 }
11 11
@@ -49,7 +49,7 @@
49 49 };
50 50
51 51 TimeAgo.prototype.updateInterval = function() {
52   - var filter, newestTime, newestTimeInMinutes, newestTimeSrc;
  52 + var filter, newestTime, newestTimeInMinutes, newestTimeInSeconds, newestTimeSrc;
53 53 if (this.$element.findAndSelf(this.options.selector).length > 0) {
54 54 if (this.options.dir === "up") {
55 55 filter = ":first";
@@ -59,15 +59,27 @@
59 59 newestTimeSrc = this.$element.findAndSelf(this.options.selector).filter(filter).attr(this.options.attr);
60 60 newestTime = this.parse(newestTimeSrc);
61 61 newestTimeInMinutes = this.getTimeDistanceInMinutes(newestTime);
62   - if (newestTimeInMinutes < 44.5 && this.startInterval !== 30000) {
63   - this.startInterval = 30000;
  62 + newestTimeInSeconds = newestTimeInMinutes * 60;
  63 + if (newestTimeInMinutes <= 1) {
  64 + if (newestTimeInSeconds <= 9 && this.startInterval !== 5000) {
  65 + this.startInterval = 5000;
  66 + return restartTimer();
  67 + } else if (newestTimeInSeconds <= 59 && this.startInterval !== 10000) {
  68 + this.startInterval = 10000;
  69 + return restartTimer();
  70 + }
  71 + } else if (newestTimeInMinutes >= 2 && newestTimeInMinutes <= 45 && this.startInterval !== 60000) {
  72 + this.startInterval = 60000;
64 73 return this.restartTimer();
65   - } else if (newestTimeInMinutes >= 44.5 && newestTimeInMinutes < 89.5 && this.startInterval !== 60000 * 15) {
66   - this.startInterval = 60000 * 15;
67   - return this.restartTimer();
68   - } else if (newestTimeInMinutes >= 89.5 && newestTimeInMinutes < 1439.5 && this.startInterval !== 60000 * 30) {
  74 + } else if (newestTimeInMinutes >= 46 && newestTimeInMinutes <= 89 && this.startInterval !== 60000 * 30) {
69 75 this.startInterval = 60000 * 30;
70 76 return this.restartTimer();
  77 + } else if (newestTimeInMinutes >= 90 && newestTimeInMinutes <= 2519 && this.startInterval !== 60000 * 60) {
  78 + this.startInterval = 60000 * 60;
  79 + return this.restartTimer();
  80 + } else if (newestTimeInMinutes >= 2520 && this.startInterval !== 60000 * 60 * 24) {
  81 + this.startInterval = 60000 * 60 * 24;
  82 + return this.restartTimer();
71 83 }
72 84 }
73 85 };
@@ -92,36 +104,48 @@
92 104 TimeAgo.prototype.getTimeDistanceInMinutes = function(absolutTime) {
93 105 var timeDistance;
94 106 timeDistance = new Date().getTime() - absolutTime.getTime();
95   - return (Math.abs(timeDistance) / 1000) / 60;
  107 + return Math.round((Math.abs(timeDistance) / 1000) / 60.0);
96 108 };
97 109
98 110 TimeAgo.prototype.distanceOfTimeInWords = function(dim) {
99   - if (dim >= 0 && dim < 0.5) {
100   - return "less than a minute";
101   - } else if (dim >= 0.5 && dim < 1.5) {
102   - return "1 minute";
103   - } else if (dim >= 1.5 && dim < 44.5) {
104   - return "" + (Math.round(dim)) + " minutes";
105   - } else if (dim >= 44.5 && dim < 89.5) {
  111 + var dis;
  112 + dis = Math.round(distanceInMinutes * 60);
  113 + if (dim <= 1) {
  114 + if (dis <= 4) {
  115 + return "less than 5 seconds";
  116 + } else if (dis >= 5 && dis <= 9) {
  117 + return "less than 10 seconds";
  118 + } else if (dis >= 10 && dis <= 19) {
  119 + return "less than 20 seconds";
  120 + } else if (dis >= 20 && dis <= 39) {
  121 + return "half a minute";
  122 + } else if (dis >= 40 && dis <= 59) {
  123 + return "less than a minute";
  124 + } else {
  125 + return "1 minute";
  126 + }
  127 + } else if (dim >= 2 && dim <= 44) {
  128 + return "" + dim + " minutes";
  129 + } else if (dim >= 45 && dim <= 89) {
106 130 return "about 1 hour";
107   - } else if (dim >= 89.5 && dim < 1439.5) {
108   - return "about " + (Math.round(dim / 60)) + " hours";
109   - } else if (dim >= 1439.5 && dim < 2519.5) {
  131 + } else if (dim >= 90 && dim <= 1439) {
  132 + return "about " + (Math.round(dim / 60.0)) + " hours";
  133 + } else if (dim >= 1440 && dim <= 2519) {
110 134 return "1 day";
111   - } else if (dim >= 2519.5 && dim < 43199.5) {
112   - return "" + (Math.round(dim / 1440)) + " days";
113   - } else if (dim >= 43199.5 && dim < 86399.5) {
  135 + } else if (dim >= 2520 && dim <= 43199) {
  136 + return "" + (Math.round(dim / 1440.0)) + " days";
  137 + } else if (dim >= 43200 && dim <= 86399) {
114 138 return "about 1 month";
115   - } else if (dim >= 86399.5 && dim < 525599.5) {
116   - return "" + (Math.round(dim / 43200)) + " months";
117   - } else if (dim >= 525599.5 && dim < 655199.5) {
  139 + } else if (dim >= 86400 && dim <= 525599) {
  140 + return "" + (Math.round(dim / 43200.0)) + " months";
  141 + } else if (dim >= 525600 && dim <= 655200) {
118 142 return "about 1 year";
119   - } else if (dim >= 655199.5 && dim < 914399.5) {
  143 + } else if (dim >= 655201 && dim <= 914400) {
120 144 return "over 1 year";
121   - } else if (dim >= 914399.5 && dim < 1051199.5) {
  145 + } else if (dim >= 914400 && dim <= 1051200) {
122 146 return "almost 2 years";
123 147 } else {
124   - return "about " + (Math.round(dim / 525600)) + " years";
  148 + return "about " + (Math.round(dim / 525600.0)) + " years";
125 149 }
126 150 };
127 151
92 src/timeago.coffee
@@ -7,7 +7,7 @@
7 7
8 8 class TimeAgo
9 9 constructor: (element, options) ->
10   - @startInterval = 30000
  10 + @startInterval = 5000
11 11 @init(element, options)
12 12
13 13 init: (element, options) ->
@@ -48,15 +48,26 @@ class TimeAgo
48 48 newestTimeSrc = @$element.findAndSelf(@options.selector).filter(filter).attr(@options.attr)
49 49 newestTime = @parse(newestTimeSrc)
50 50 newestTimeInMinutes = @getTimeDistanceInMinutes(newestTime)
51   - if newestTimeInMinutes < 44.5 and @startInterval != 30000
52   - @startInterval = 30000
  51 + newestTimeInSeconds = newestTimeInMinutes * 60
  52 + if newestTimeInMinutes <= 1
  53 + if newestTimeInSeconds <= 9 and @startInterval != 5000 #5 seconds
  54 + @startInterval = 5000
  55 + restartTimer()
  56 + else if newestTimeInSeconds <= 59 and @startInterval != 10000 #10 seconds
  57 + @startInterval = 10000
  58 + restartTimer()
  59 + else if newestTimeInMinutes >= 2 and newestTimeInMinutes <= 45 and @startInterval != 60000 #1 minute
  60 + @startInterval = 60000
53 61 @restartTimer()
54   - else if newestTimeInMinutes >= 44.5 and newestTimeInMinutes < 89.5 and @startInterval != 60000 * 15
55   - @startInterval = 60000 * 15
56   - @restartTimer()
57   - else if newestTimeInMinutes >= 89.5 and newestTimeInMinutes < 1439.5 and @startInterval != 60000 * 30
  62 + else if newestTimeInMinutes >= 46 and newestTimeInMinutes <= 89 and @startInterval != 60000 * 30 #30 minutes
58 63 @startInterval = 60000 * 30
59 64 @restartTimer()
  65 + else if newestTimeInMinutes >= 90 and newestTimeInMinutes <= 2519 and @startInterval != 60000 * 60 #1 hour
  66 + @startInterval = 60000 * 60
  67 + @restartTimer()
  68 + else if newestTimeInMinutes >= 2520 and @startInterval != 60000 * 60 * 24 #1 day
  69 + @startInterval = 60000 * 60 * 24
  70 + @restartTimer()
60 71
61 72 timeAgoInWords: (timeString) ->
62 73 absolutTime = @parse(timeString)
@@ -73,50 +84,47 @@ class TimeAgo
73 84
74 85 getTimeDistanceInMinutes: (absolutTime) ->
75 86 timeDistance = new Date().getTime() - absolutTime.getTime()
76   - (Math.abs(timeDistance) / 1000) / 60
  87 + Math.round((Math.abs(timeDistance) / 1000) / 60.0)
77 88
78 89 distanceOfTimeInWords: (dim) ->
79   - # Words need to be the same with Rails
80   - # 0 <-> 29 secs # => less than a minute
81   - # 30 secs <-> 1 min, 29 secs # => 1 minute
82   - # 1 min, 30 secs <-> 44 mins, 29 secs # => [2..44] minutes
83   - # 44 mins, 30 secs <-> 89 mins, 29 secs # => about 1 hour
84   - # 89 mins, 30 secs <-> 23 hrs, 59 mins, 29 secs # => about [2..24] hours
85   - # 23 hrs, 59 mins, 30 secs <-> 41 hrs, 59 mins, 29 secs # => 1 day
86   - # 41 hrs, 59 mins, 30 secs <-> 29 days, 23 hrs, 59 mins, 29 secs # => [2..29] days
87   - # 29 days, 23 hrs, 59 mins, 30 secs <-> 59 days, 23 hrs, 59 mins, 29 secs # => about 1 month
88   - # 59 days, 23 hrs, 59 mins, 30 secs <-> 1 yr minus 1 sec # => [2..12] months
89   - # 1 yr <-> 1 yr, 3 months # => about 1 year
90   - # 1 yr, 3 months <-> 1 yr, 9 months # => over 1 year
91   - # 1 yr, 9 months <-> 2 yr minus 1 sec # => almost 2 years
92   - # 2 yrs <-> max time or date # => (same rules as 1 yr)
93   -
94   - if dim >= 0 and dim < 0.5
95   - "less than a minute"
96   - else if dim >= 0.5 and dim < 1.5
97   - "1 minute"
98   - else if dim >= 1.5 and dim < 44.5
99   - "#{ Math.round(dim) } minutes"
100   - else if dim >= 44.5 and dim < 89.5
  90 + #TODO support i18n.
  91 + dis = Math.round(distanceInMinutes * 60) #distance in seconds
  92 +
  93 + if dim <= 1
  94 + if dis <= 4
  95 + "less than 5 seconds"
  96 + else if dis >= 5 and dis <= 9
  97 + "less than 10 seconds"
  98 + else if dis >= 10 and dis <= 19
  99 + "less than 20 seconds"
  100 + else if dis >= 20 and dis <= 39
  101 + "half a minute"
  102 + else if dis >= 40 and dis <= 59
  103 + "less than a minute"
  104 + else
  105 + "1 minute"
  106 + else if dim >= 2 and dim <= 44
  107 + "#{ dim } minutes"
  108 + else if dim >= 45 and dim <= 89
101 109 "about 1 hour"
102   - else if dim >= 89.5 and dim < 1439.5 #23 hrs, 59 mins, 29 secs
103   - "about #{ Math.round(dim / 60) } hours"
104   - else if dim >= 1439.5 and dim < 2519.5 #41 hrs, 59 mins, 29 secs
  110 + else if dim >= 90 and dim <= 1439
  111 + "about #{ Math.round(dim / 60.0) } hours"
  112 + else if dim >= 1440 and dim <= 2519
105 113 "1 day"
106   - else if dim >= 2519.5 and dim < 43199.5 #29 days, 23 hrs, 59 mins, 29 secs
107   - "#{ Math.round(dim / 1440) } days"
108   - else if dim >= 43199.5 and dim < 86399.5 #59 days, 23 hrs, 59 mins, 29 secs
  114 + else if dim >= 2520 and dim <= 43199
  115 + "#{ Math.round(dim / 1440.0) } days"
  116 + else if dim >= 43200 and dim <= 86399
109 117 "about 1 month"
110   - else if dim >= 86399.5 and dim < 525599.5 #1 yr minus half minute
111   - "#{ Math.round(dim / 43200) } months"
112   - else if dim >= 525599.5 and dim < 655199.5 #1 yr, 3 months
  118 + else if dim >= 86400 and dim <= 525599 #1 yr
  119 + "#{ Math.round(dim / 43200.0) } months"
  120 + else if dim >= 525600 and dim <= 655200 #1 yr, 3 months
113 121 "about 1 year"
114   - else if dim >= 655199.5 and dim < 914399.5 #1 yr, 9 months
  122 + else if dim >= 655201 and dim <= 914400 #1 yr, 9 months
115 123 "over 1 year"
116   - else if dim >= 914399.5 and dim < 1051199.5 #2 yr minus half minute
  124 + else if dim >= 914400 and dim <= 1051200 #2 yr minus half minute
117 125 "almost 2 years"
118 126 else
119   - "about #{Math.round(dim / 525600)} years"
  127 + "about #{ Math.round(dim / 525600.0) } years"
120 128
121 129 $.fn.timeago = (options = {}) ->
122 130 @each ->

0 comments on commit 15c47c7

Please sign in to comment.
Something went wrong with that request. Please try again.