Skip to content

Instantly share code, notes, and snippets.

@liddiard
Created May 31, 2014 04:18
Show Gist options
  • Save liddiard/0b78adaeea11bf309704 to your computer and use it in GitHub Desktop.
Save liddiard/0b78adaeea11bf309704 to your computer and use it in GitHub Desktop.
(function($) {
/*!
A very simplified version of:
Name: Reading Time
Dependencies: jQuery
Author: Michael Lynch
Author URL: http://michaelynch.com
Date Created: August 14, 2013
Date Updated: January 24, 2014
Licensed under the MIT license
*/
$.fn.readingTime = function(selector) {
//return if no element was bound
//so chained events can continue
if(!this.length) {
return this;
}
//define element
var el = $(this);
// words per minute
var wpm = 270;
// calculate the reading time
var reading_time = Math.round(el.text().split(' ').length / wpm);
// don't display anything if reading time is less that 30 secs
if (reading_time < 1)
return this;
else
$(selector).html(reading_time + ' min. read &ndash; ');
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment