Forum Discussion

PaquitaFerriSor's avatar
PaquitaFerriSor
Community Member
28 days ago

Error when restarting in LMS

Hi,

The problem is related to the JS timer that I have included in my course... It works perfectly and as expected throughout the course, but if you leave to return later the timer is stopped and does not activate again. manually with activators or automatically with the code...

Here is the JS code:

const player = GetPlayer();
let totalSeconds = player.GetVar("timer") || 0;
let sec, min, hour;

function formatTime(unit) {
    return unit < 10 ? '0' + unit : unit;
}

function startTimer() {
    totalSeconds += 1;
    sec = totalSeconds % 60;
    min = Math.floor((totalSeconds / 60) % 60);
    hour = Math.floor(totalSeconds / 3600);

    let formattedSec = formatTime(sec);
    let formattedMin = formatTime(min);
    let formattedHour = formatTime(hour);

    // Actualizar variables en Storyline
    player.SetVar("formattedSeconds", formattedSec);
    player.SetVar("formattedMinutes", formattedMin);
    player.SetVar("formattedHours", formattedHour);
    player.SetVar("timer", totalSeconds);

    // Guardar el valor del temporizador en el LMS
    player.SetVar("LMSData", totalSeconds);
}

// Recuperar el tiempo almacenado del LMS al cargar el curso
let savedTime = player.GetVar("LMSData");
if (savedTime) {
    totalSeconds = savedTime;
}

// Asegurar que el temporizador se reanuda al recargar el curso
function initializeTimer() {
    if (!player.GetVar("timerId")) {
        let timerId = setInterval(startTimer, 1000);
        player.SetVar("timerId", timerId);
    }
}

// Comprobamos si el temporizador ya ha sido iniciado anteriormente
initializeTimer();

// Evento adicional para garantizar el inicio automático del temporizador en caso de recarga
player.on('storyline:load', function() {
    initializeTimer();
});

I've got the next variables previously created on storyline:

formattedHours

formattedMinutes

formattedSeconds

timer (total seconds spent on course)

The timer when you re-enter still keeps the progress but cannot continue counting the time...

I have been trying different versions of the code and logic for a week and none of them manage to solve this error when exiting and entering, I suspect that it may be related to how it handles the js and the storyline variables within the LMS once it has to save and recover the progress but I find very little information about it...

No RepliesBe the first to reply