Skip to main navigation Skip to main content Skip to page footer
Calculating the estimated reading time for blog articles: A tutorial

Calculating the estimated reading time for blog articles: A tutorial

Have the article read aloud.

Loading the Elevenlabs Text to Speech AudioNative Player...
| Web Development | Estimated reading time : min.

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.

Integration of the code

To use this feature, you must follow these steps:

1. integrate JavaScript code

Insert the provided JavaScript code into a JavaScript file or integrate it directly into a <script> tag within your HTML document:

<script>
// Here is the JavaScript for calculating and displaying the reading time
</script>

2. Prepare HTML elements

Your article HTML should contain at least two elements: One for the text content and one in which the reading time is displayed. For example:

<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>

3. Call up JavaScript code

Make sure that the updateReadingTime function is called as soon as the text content has been loaded. If you insert the code directly into the HTML document, this is done automatically at the end of the script.

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();
});
Back

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!

Trage dich hier ein, um den Newsletter zu erhalten.