Display system time in Storyline 3 using Javascript
Apr 29, 2021
By
Chandi Jaya
Hi,
I have a Storyline3 project which needs to display the system time, always updating.
I used the following code, but it didn't work.
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
// add a zero in front of numbers<10
m = checkTime(m);
s = checkTime(s);
var p=GetPlayer();
p.SetVar("h",h);
var q=GetPlayer();
q.SetVar("m",m);
var r=GetPlayer();
r.SetVar("s",s);
var t = setTimeout(function(){ startTime() }, 1000);
}
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
Does anyone know the exact code for this?
Chandi
1 Reply
A few mistakes in your script. Biggest one is you never call startTime( ); .. thus the script never gets executed. Your timer is set inside the startTime function so also never gets called.
Do try to make a habit of it using either alert("Now im here"); or console.log("and now im here"); to debug your code. If you add calls like this at keypoints in your code its easy to debug and see where and what goes wrong. Without that...its belly-staring.
Another mistake is calling the player 3 times. I am not sure whether it stops your script but it sure aint nice.
Like this the script works:
var player = GetPlayer();
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
// add a zero in front of numbers<10
m = checkTime(m);
s = checkTime(s);
console.log("m: "+m+" |s: "+s);
player.SetVar("h",h);
player.SetVar("m",m);
player.SetVar("s",s);
var t = setTimeout(function(){ startTime() }, 1000);
}
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
startTime();
For your ease adding a working 360 and Storyline 3 version.
Kind regards,
Math Notermans