Copy link to clipboard
Copied
Hello everybody,
I'm struggling with a task I have to complete, any help is very much welcome at this point.
Context: at the end of the elearning I'm developing, I need to include a slide (that will be printed for archives and payment and stuff ) with the duration the trainee took. Each trainee is going to have a different time due to questions, quiz etc. I also like to say that I'm more a specialist in the stuff I teach then in javascript or advanced actions.
What I did: on the page where I want to display the time, I trigger an advanced action.
In it, I assign the variable FinalTime with cpInfoElapsedTimeMS (it would be so nice if it had immediate formatting options as for the current date ).Now, I need this ElapsedTime to look readable, not just in seconds but as a human timing as for a video.
So then in the advanced action I try to execute a Javascript which is:
var millisec = window.cpAPIInterface.getVariableValue("FinalTime");
function getMyTime(millisec) {
var seconds = (millisec / 1000).toFixed(0);
var minutes = Math.floor(seconds / 60);
var hours = "";
if (minutes > 59) {
hours = Math.floor(minutes / 60);
hours = (hours >= 10) ? hours : "0" + hours;
minutes = minutes - (hours * 60);
minutes = (minutes >= 10) ? minutes : "0" + minutes;
}
seconds = Math.floor(seconds % 60);
seconds = (seconds >= 10) ? seconds : "0" + seconds;
if (hours != "") {
return hours + ":" + minutes + ":" + seconds;
}
return minutes + ":" + seconds;
}
window.cpAPIInterface.setVariableValue("Duration",getMyTime);
Well, let's add that I declared all my variables in CP and that on my slide I inserted $$Duration$$
The answer I receive is 0 when running everything. So can anybody tell me what I did wrong, and ideally tell me how to do it right?
Thanks so much for reading.
So, I finally found a way to do what I wanted, I share it here in case someone else would be interested.
On the one slide where I want to see the timing, on slide enter I run an advanced action. During my experimentation, I found out that I wanted to do as little as possible with javascript, being a complete newbee.
So my conditional action finally look like this:
The code executes JS for which the code is every time pretty much the same this is the first one:
var rounding = window.cpAPIInterface
...Copy link to clipboard
Copied
Did you define the user variable in Captivate? Hope you inserted it with the X button from the Properties to make sure the exact name of the variable is inserted. I have created similar calculations in Captivate, too lazy to go testing scripts. I hope one of the JS experts chimes in.
Copy link to clipboard
Copied
Hi, thanks for replying, Yes to the user variables, and yes to inserting with the X button in the Properties.
I don't seem to see your last sentence completely, but searching the web I haven't managed to find an entire code snippet doing just what I want.
Unfortunately, I'm still stuck.
Copy link to clipboard
Copied
Have a lot on my plate today, should screen that JS...
Which event is used to fire the action? Captivate's actions are event driven. Attach that action to a button on the slide, is it giving a correct result when the button is clicked?
Did you see my blog post:
Copy link to clipboard
Copied
Lilybiri, thank you for your efforts and answers.
Yes I checked out your blog, thank you. The issue I have is that I don't just need seconds to display but a timing like "1 hour 15 minutes" or "1:15".
I tried to do that with my javascript but as javascript in itself is a bit obscure to me, its utilization in CP seems even more dark to me.
I tried to solve the matter just with conditional advanced actions but something failed and I always got a result back that must be some kind of standard error (it a negative huge number -15xxxxxxxx - I don't remember all the digits).
So basically, I tried and tried again, and still continue, I still hope that I'll figure it out finally .
Copy link to clipboard
Copied
If you want to have this happening on each frame, you'll have to create more events. You never answered my question about 'which event is triggering the action'? That was the reason why I pointed to that blog.
I often use the timing interaction just to show time in courses. Did you try that?
Copy link to clipboard
Copied
So, I finally found a way to do what I wanted, I share it here in case someone else would be interested.
On the one slide where I want to see the timing, on slide enter I run an advanced action. During my experimentation, I found out that I wanted to do as little as possible with javascript, being a complete newbee.
So my conditional action finally look like this:
The code executes JS for which the code is every time pretty much the same this is the first one:
var rounding = window.cpAPIInterface.getVariableValue("seconds");
var rounding = Math.floor(rounding);
window.cpAPIInterface.setVariableValue("Duration", rounding);
For the next JS the codes are almost the same, what changes is the name of the input and output variables.
And finally the MOST IMPORTANT thing, the one it took me so much time and frustration to find out: to test all of this, a preview of the project is not enough! The project must be published and active X activated in the browser. Seems simple and stupid but I had to discover this bit of info the hard way.