Duration, display minutes & seconds

Dec 03, 2019

Hello (again!)

I've created multiple slides that capture the total duration taken to answer the slide correctly, and this seems to work ok using the code I was able to find online. However, I have no idea how to now change it so that it shoes minutes and seconds.

I've attached the story file for reference.

This is the code I've used:

var player = GetPlayer();

var Start = player.GetVar("gsStart");
var End = player.GetVar("gsEnd");

var nDuration = Math.round((End - Start) / 1000 / 60);

player.SetVar("gsDuration" , nDuration);

6 Replies
OWEN HOLT
  1. Get the total number of seconds by calculating the difference between the start and stop time and stripping away any milliseconds:
    Change the line "var nDuration = Math.round((End - Start) / 1000 / 60);" to var nDuration = Math.round((End - Start) / 1000);
  2. Calculate if this took longer than an hour:
    Add the line: "var hours = Math.floor(nDuration / 3600); //3600 represents the number of seconds in one hour
  3. Calculate the minutes using the JS Remainder / Modulus (%) in the prior calculation, but dividing by 60 instead of 3600:
    Add the line: var minutes = Math.floor((nDuration % 3600) / 60); //60 represents the number of seconds in a minute.
  4. Calculate the remainder of the prior calculation once again using the Remainder Modulus:
    Add the line: var seconds = (nDuration % 3600) % 60;
  5. If you wanted to display this in HH:MM:SS format, you need to determine if any of the above variables are a single digit and, if so, add a leading "0".
    Add the following lines to the code:
    if (hours < 10) {hours = "0"+hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
  6. Put them all together:
    Add the following line of code: nDuration = hours+':'+minutes+':'+seconds;

Your final code should look something like the following: 

var player = GetPlayer();

var Start = player.GetVar("gsStart");
var End = player.GetVar("gsEnd");

var nDuration = Math.round((End - Start) / 1000);
var hours = Math.floor(nDuration / 3600);
var minutes = Math.floor((nDuration % 3600) / 60);
var seconds = (nDuration % 3600) % 60;
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
nDuration = hours+':'+minutes+':'+seconds;

player.SetVar("gsDuration" , nDuration);

and will return the time in a format that looks something like this 00:05:27

Blue Kearsley

Hi Owen,

Thanks for your reply. I've added the code as stated, but it just displays 0 minutes.

After speaking to a colleague, we found that I needed to update my SL variable to text, instead of a number. This then seemed to work as expected. I've tweaked it so it's a string, rather than clock format.

Thank you so much!

My final code:

var player = GetPlayer();

var Start = player.GetVar("gsStart");
var End = player.GetVar("gsEnd");

var nDuration = Math.round((End - Start) / 1000);
var minutes = Math.floor((nDuration % 3600) / 60);
var seconds = (nDuration % 3600) % 60;

nDuration = minutes+' minute(s) '+seconds + ' seconds';

player.SetVar("gsDuration" , nDuration);

Blue Kearsley

Ah, through testing, I'm finding that if I click back to the slides with this calculation on, the timer still calculates but goes into minus amounts. So to correct that, my colleague was able to create an if statement that will keep the initial value, regardless how many times the slide is viewed. I had to create another variablle, and will do so for each slide this code is used on. My code now looks like this:

var player = GetPlayer();

var D_Result = player.GetVar("D_Result");

if (D_Result == '')
{
var Start = player.GetVar("gsStart");
var End = player.GetVar("gsEnd");

var nDuration = Math.floor((End - Start) / 1000);
var minutes = Math.floor((nDuration % 3600) / 60);
var seconds = (nDuration % 3600) % 60;

D_Result = minutes+' minute(s) '+seconds + ' seconds';

player.SetVar("D_Result" , D_Result);
}

This discussion is closed. You can start a new discussion or contact Articulate Support.