Calculating the estimated reading time for blog articles: A tutorial
Have the article read aloud.
If you want to give your readers additional guidance and show them roughly how much time they should plan to spend reading your post, giving them an estimated reading time is a handy feature. In this blog article, I'll show you how you can create this added value on your website with a simple piece of JavaScript, regardless of which content management system or technology you use.
What the JavaScript code does
This code calculates the estimated reading time based on the number of words in a text. We assume that an average reader reads around 200 words per minute. You can adjust this value to the reading habits of your target group if necessary.
Essentially, the code consists of two parts:
- The calculation function (calculateReadingTime): This function takes a text string, counts the words and divides this number by the average reading speed. The result is rounded up to the nearest whole number to obtain the minutes of reading time.
- The update function (updateReadingTime): Here, the actual text is retrieved from an element with the .news-text-wrap class. After the reading time has been calculated, it is displayed in an element with the class .estimated-reading-time_inner.
<div class="estimated-reading-time">
Estimated reading time: <span class="estimated-reading-time_inner"></span> minutes
</div>
<div class="news-text-wrap">
<!-- Here is the text of the article -->
</div>
Make adjustments
- Adjusted reading speed: If you know that your readers read faster or slower, you can change the wordsPerMinute value accordingly.
- Responsive display: Make sure that the reading time display also looks and works well on mobile devices.
Conclusion
Indicating an estimated reading time on your page is a simple but effective way to provide readers with an additional service and exude professionalism at the same time. This little JavaScript snippet can be used anywhere, regardless of the platform used.
The JavaScript code
document.addEventListener('DOMContentLoaded', function() {
// Estimated reading time for blog articles
// Calculates the estimated reading time based on the number of words
function calculateReadingTime(text) {
const wordsPerMinute = 200; // Average reading speed
const words = text.trim().split(/\s+/).length; // Count the words
const readingTime = Math.ceil(words / wordsPerMinute);
return readingTime;
}
// Updates the display of the estimated reading time
function updateReadingTime() {
const newsTextWrap = document.querySelector('.news-text-wrap');
// Checks if the element exists
if (newsTextWrap) {
const textContent = newsTextWrap.innerText;
const readingTime = calculateReadingTime(textContent);
const readingTimeDisplay = document.querySelector('.estimated-reading-time_inner');
// Only updates if the target element exists
if (readingTimeDisplay) {
readingTimeDisplay.innerText = `${readingTime}`;
}
}
}
updateReadingTime();
});
Who writes here?
Hi, I am Wolfgang.
Since 2006, I've been diving deep into the fascinating world of TYPO3 - it's not only my profession, but also my passion. My path has taken me through countless projects, and I have created hundreds of professional video tutorials focusing on TYPO3 and its extensions. I love unraveling complex topics and turning them into easy-to-understand concepts, which is also reflected in my trainings and seminars.
As an active member of the TYPO3 Education Committee, I am committed to keeping the TYPO3 CMS Certified Integrator exam questions current and challenging. Since January 2024, I am proud to be an official TYPO3 Consultant Partner!
But my passion doesn't end at the screen. When I'm not diving into the depths of TYPO3, you'll often find me on my bike, exploring the picturesque trails around Lake Constance. These outdoor excursions are my perfect balance - they keep my mind fresh and always provide me with new ideas.
Der TYPO3 Newsletter
TYPO3-Insights direkt in dein Postfach!
Hol dir monatliche Updates, praktische Tipps und spannende Fallstudien.
Übersichtlich, zeitsparend, ohne Spam.
Bist du dabei? Jetzt für den Newsletter anmelden!