How Does SMIL Integrate with JavaScript in Time Containers?

Synchronized Multimedia Integration Language (SMIL) coordinates complex, time-based media layouts, while JavaScript provides the programmatic interface needed to alter that timeline dynamically. By combining SMIL time containers—such as sequential (<seq>), parallel (<par>), and exclusive (<excl>) groupings—with standard Document Object Model (DOM) methods and the ElementTimeControl interface, developers can start, stop, reschedule, or restructure time containers on the fly.

Core SMIL Time Containers

SMIL organizes media elements along a temporal graph using specialized container elements or attributes:

In host environments like SVG or HTML+SMIL profiles, these behaviors can also be attached to generic grouping elements using the timeContainer="par|seq|excl" attribute.

The ElementTimeControl Interface

SMIL exposes native timing methods directly on elements that implement the ElementTimeControl or SVGAnimationElement interfaces:

// Access a SMIL time container or animation element
const container = document.getElementById("introSequence");

// Force the container to begin immediately
container.beginElement();

// Schedule container termination 3.5 seconds from now
container.endElementAt(3.5);

Dynamic Attribute Mutation via DOM APIs

JavaScript can alter synchronization rules at runtime using standard setAttribute() and removeAttribute() calls. Changing timing attributes modifies how SMIL evaluates the synchronization graph:

const mainPar = document.getElementById("parallelGroup");

// Dynamically shorten overall duration
mainPar.setAttribute("dur", "4s");

// Re-link container begin time to an alternate event
mainPar.setAttribute("begin", "userTrigger.click; nextBtn.click + 1s");

Structural Mutation of the Time Graph

Because SMIL schedules items based on DOM hierarchy, altering the DOM tree directly transforms playback order:

const seqContainer = document.getElementById("playlistSeq");
const firstTrack = seqContainer.firstElementChild;

// Move first item to the back of the queue
if (firstTrack) {
  seqContainer.appendChild(firstTrack);
}

Global Timeline Synchronization

In SVG-based SMIL implementations, the root <svg> element exposes global timeline methods that pause, resume, or seek the entire synchronization tree:

Using these methods in tandem with DOM events gives JavaScript full real-time control over declarative SMIL media schedules.