TImer within a course (Javascript help?)

Jul 29, 2020

Bit of an unusual one.

I have created a Treasure Hunt in Storyline. Players follow clues to walk around our venue, entering number or text entries to progress to the next clue (slide).

I thought I was being clever and added some Javascript I found on the forum to record their time in hours, minutes and seconds.

BUT - the players are using their phones. It's now obvious that they will be locking their phones whilst walking to the next location, using other apps like googlemaps etc. All of which minimise the browser and pause the timer. Argh!

Its all remote so we can't have someone physically there to record start and stop times. I originally had an email populated with the time and sent over at the end of the hunt. But we now know that it isn't an accurate time.


Does anyone have any suggestions on how to record a time, bearing in mind it will be published for web and all mobile?


My idea so far: Is it possible to use JS to record and store the time at the start of the game, then do the same again at the end. Then use the email trigger I have created to send these two times back to base? 
I guess if it was I could do more work to calculate the difference between both times. 

Any help of further suggestions massively appreciated!



4 Replies
Dave Cox

Yes, what you want to do is possible, Getting current time with Javascript can be done, but it does take a bit of work.  The following bit of code will get the current time in a readable format, and place it in a Storyline variable which I called "StoryLineVariable". Be sure to update this to your text variable in Storyline.

var today = new Date(); // get the date in milliseconds
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time; // put the two strings together
var player = GetPlayer(); // get the storyline player function
player.SetVar("StoryLineVariable",dateTime); // send the date to Storyline

If you want to calculate a difference, you can do that too. First you would need to save the first date in a variable as milliseconds.

var player = GetPlayer(): // get the storyline player function
var date1 = new Date(); // get the first date
player.SetVar("StoryLineVariable2",date1); // send the first date to Storyline

Then you would need to retrieve that information to use in your calculation for a difference when you get the new date.

var player = GetPlayer(); // get the storyline player function
var date1 = ("StorylineVariable2"); // retrieve the date save earlier
var date2 = new Date(); // get the second date
var diff = date2 - date1; // get the differences in milliseconds
// then calculate the time difference in hours, minutes and seconds
var msec = diff; // Put the diff in another variable so we can work with it
var hh = Math.floor(msec / 1000 / 60 / 60); // calculate the hours
msec -= hh * 1000 * 60 * 60; // Remove the hours from the variable
var mm = Math.floor(msec / 1000 / 60); // Get the minutes
msec -= mm * 1000 * 60; // Remove the minutes
var ss = Math.floor(msec / 1000);
var CalculatedTime = hh+':'+mm+':'+ss; Put them together as hh:mm:ss
player.SetVar(StoryLineVariable3",CalculatedTime); // Send the string to StoryLine

I hope this helps you. In Javascript the "//" indicates that the rest of the line is a comment, and I used that to comment my code for you so you can see what is happening.


Martin Sinclair

Ah thank you son much Dave!

Really appreciate it. Think I am just about there. Complete Javascript novice but I've learnt a lot in the last 24hours!

I've ended up with a combination of the above and built in variables / triggers.

Essentially used JS to grab the time when the game begins, then again at the end. With those two times, I've converted the hours from the first time into minutes (by x60) then adding on the minutes from the first time. Then it does the same with the second time.


Then use a bit of JS again to work out the difference.


Really interesting, if a little bit of trial and error. Testing it now, thanks for your help!

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