Forum Discussion

ronald-hicks's avatar
ronald-hicks
Community Member
1 hour ago

2-minute snippet: countdown timer for timed quizzes

Timed quizzes come up constantly, and the usual answers are either "buy a template" or "fake it with slide timers." Here's the middle path — a real countdown that drives Storyline variables, so your timeout logic stays in triggers where it belongs:

// Countdown timer driving Storyline variables. Timed quizzes.
// Set TimerMinutes, and the snippet counts down once per second, updating
// TimerRemaining (seconds left), Timer_Display ("4:59"), and Timer_Done
// ("yes" when time runs out).
//
// SETUP (1 minute) — variables FIRST:
//   1. Create TimerMinutes (Number, 5 — your time limit), TimerRemaining
//      (Number, 0), Timer_Display (Text, blank), and Timer_Done (Text, "no").
//   2. Paste this into a trigger: Action Execute JavaScript,
//      When the timeline starts on the quiz slide.
//   3. Put this trigger ABOVE any other trigger on the same event.
//
// Show %Timer_Display% on the slide. Add a trigger: Jump to your timeout /
// results slide When Timer_Done changes to "yes". Re-running the slide
// resets the timer cleanly — the old interval is cleared first.
(function () {
  var player = GetPlayer();
  if (window.FXTIMER) { clearInterval(window.FXTIMER); window.FXTIMER = null; }

  var mins = Number(player.GetVar("TimerMinutes"));
  if (isNaN(mins) || mins <= 0) mins = 5;
  var left = Math.round(mins * 60);

  function show() {
    var m = Math.floor(left / 60);
    var s = left % 60;
    player.SetVar("TimerRemaining", left);
    player.SetVar("Timer_Display", m + ":" + (s < 10 ? "0" : "") + s);
  }

  player.SetVar("Timer_Done", "no");
  show();

  window.FXTIMER = setInterval(function () {
    left = left - 1;
    if (left <= 0) {
      left = 0;
      clearInterval(window.FXTIMER);
      window.FXTIMER = null;
      player.SetVar("Timer_Done", "yes");
    }
    show();
  }, 1000);
})();

Set `TimerMinutes`, show `%Timer_Display%` on screen, and jump to your timeout slide when `Timer_Done` changes to `yes`. One trigger on the first quiz slide, one "when variable changes" jump, done.

Two honest caveats from the trenches: browsers throttle timers in background tabs, so this drifts if the learner tabs away — it's for practice and low-stakes timing, not proctored exams. And re-running the trigger resets the clock, so don't put it on a slide learners revisit mid-quiz unless a reset is what you want.

Test in Preview with F12 open — F12, Console tab, frame dropdown, preview.html iframe, then paste. Temporarily set `TimerMinutes` to 1 and watch it run.

No RepliesBe the first to reply