Adding a timer to an H5P activity in Moodle

Adding a timer to an H5P activity in Moodle

by Andrew Dunn -
Number of replies: 1
Hi everyone.

I thought I'd share this in case it's useful to others. I found a method to wrap an H5P in an HTML container and to add a timer so that the H5P is hidden after the time expires. Here's the HTML (which I just added to a Moodle page):

<strong>Timed Activity in H5P</strong>

<div id="timer-display"></div>

<div id="h5p-container"></div>

//get the path to your local resizer script from the H5P iframe embed code and add it below

<script src="<Path to h5p-resizer.js>" charset="UTF-8"></script> 

<script>

    // Set the time limit in seconds

    var timeLimit = 30; // 30 seconds


    // Get the container elements

    var container = document.getElementById("h5p-container");

    var timer = document.getElementById("timer-display");


    // Create the iframe element

    var iframe = document.createElement("iframe");


    // Set the source URL of the iframe - edit this with the URL of your H5P in Moodle

    iframe.src = "<URL to your H5P in Moodle goes here>";


    // Set any additional attributes for the iframe - you can get these from the iframe code generated by the H5P element

    iframe.setAttribute("width", "xxx");

    iframe.setAttribute("height", "xxx");


    // Append the iframe to the container - this makes the H5P display on the page

    container.appendChild(iframe);


    // Start the timer

    var countdown = setInterval(function() {

        // Display the remaining time in minutes and seconds

        var minutes = Math.floor(timeLimit / 60);

        var seconds = timeLimit % 60;


        // Update the timer element with the remaining time

        timer.innerHTML = "<p>Time remaining: " + minutes + "m " + seconds + "s</p>";


        // Check if the time has run out

        if (timeLimit <= 0) {

            // Stop the timer

            clearInterval(countdown);


            // Time's up - replace the H5P with a new message

            container.innerHTML = "<p>Time's up!</p>";

        }


        // Decrease the time limit by 1 second

        timeLimit--;

    }, 1000);

</script>



Average of ratings: Useful (3)
In reply to Andrew Dunn

Re: Adding a timer to an H5P activity in Moodle

by Stephen Lea -
This is great thanks.  I've adapted it a little so it gives learners a 30-minute timer each time they access an activity - I wanted it for an escape room activity.
When the timer is up, they are redirected to another activity page.  I added the JavaScript in the activity description  ... 

<h1 style="text-align : center;"><br></h1>

<p></p>

<h1 id="m-timer" style="text-align : center;"></h1>

<p><br></p>


<script>

    var timeLimit = 1800;


    var countdown = setInterval(function() {

        // Display the remaining time in minutes and seconds

        var minutes = Math.floor(timeLimit / 60);

        var seconds = timeLimit % 60;


        document.getElementById("m-timer").innerHTML = "Remaning Time : " + minutes + " : " + seconds;


        // Check if the timer has run out

        if (timeLimit <= 0) {

            clearInterval(countdown);

            document.getElementById("m-timer").innerHTML = "Times Up!!";

            location.replace("URL to Redirect To ...")

            clearInterval(countdown);

        }


        // Activate the timer

        timeLimit--;

    }, 1000);

</script>