Calculate Article Read Time Using Javascript

Read time will show users how much does it take to complete this article by avg human. We can set speed in our configuration.

Calculate Read Time

We will do this in two steps.
1. Find number of words in the article.
2. Apply speed for the word count, which gives us time.

Finding Word Count


var str = $('article').text();
var words = str.split(" ");
var count = words.length;

Calculate Time


var time = count/speed;

So, putting two steps together, we should have something


$(function(){
var str = $('article').text();
var speed = 200, time =0, msg ;
var words = str.split(" ");
time = words.length/speed;
console.log(time);
if(time < 1) {
 msg = "< 1 min read";
}
else {
 msg = Math.round(time) + " min read
}
$("#readTime").text(msg);
})