Skip to main navigation Skip to main content Skip to page footer
Berechnung der geschätzten Lesezeit für Blogartikel: Ein Tutorial

Berechnung der geschätzten Lesezeit für Blogartikel: Ein Tutorial

Leer artículos en voz alta

Loading the Elevenlabs Text to Speech AudioNative Player...
| Web Development | Tiempo estimado de lectura : min.

Wenn du deinen Lesern eine zusätzliche Orientierung bieten möchtest und ihnen zeigen willst, wie viel Zeit sie in etwa für das Lesen deines Beitrags einplanen sollten, ist die Angabe einer geschätzten Lesezeit ein praktisches Feature. In diesem Blogartikel zeige ich dir, wie du mit einem simplen Stück JavaScript diesen Mehrwert auf deiner Website schaffen kannst, unabhängig davon, welches Content Management System oder welche Technologie du verwendest.

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

¿Quién escribe aquí?

Hola, soy Wolfgang.

Desde 2006, he estado buceando profundamente en el fascinante mundo de TYPO3 - no es sólo mi profesión, sino también mi pasión. Mi camino me ha llevado a través de innumerables proyectos, y he creado cientos de video tutoriales profesionales centrados en TYPO3 y sus extensiones. Me encanta desentrañar temas complejos y convertirlos en conceptos fáciles de entender, lo que también se refleja en mis formaciones y seminarios.

Como miembro activo del Comité de Educación TYPO3, estoy comprometido a mantener las preguntas del examen TYPO3 CMS Certified Integrator actualizadas y desafiantes. ¡Desde enero de 2024 estoy orgulloso de ser un Consultor Partner oficial de TYPO3!

Pero mi pasión no termina en la pantalla. Cuando no estoy buceando en las profundidades de TYPO3, a menudo me encontrarás en mi bicicleta, explorando los pintorescos senderos alrededor del lago Constanza. Estas excursiones al aire libre son mi equilibrio perfecto: mantienen mi mente fresca y siempre me aportan nuevas 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.