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:
<par>(Parallel): Plays all child elements simultaneously, finishing when all children conclude or when explicitly terminated by attributes likeendsync.<seq>(Sequential): Plays child items one after another in order, where each element begins as the previous one ends.<excl>(Exclusive): Allows only one child element to be active at a time, pausing or stopping running elements when a sibling triggers.
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:
beginElement(): Triggers the container or animation element to start immediately in the local timeline.beginElementAt(offset): Schedules the start time relative to the current clock with a numeric offset in seconds.endElement(): Forces the element to finish its active duration immediately.endElementAt(offset): Schedules element termination after a designated delay.
// 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:
- Modifying Durations: Adjusting the
durattribute dynamically speeds up or extends playback intervals. - Re-anchoring Begin/End Syncs: Setting
beginto an element-based identifier (e.g.,containerB.begin = "containerA.end + 2s") reconfigures dependency arcs between containers. - Loop Control: Mutating
repeatCountorrepeatDurto integer values or"indefinite"controls container iteration cycles.
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:
- Reordering Sequence Items: Moving child nodes
within a
<seq>container immediately alters the execution sequence for future iterations. - Injecting Dynamic Nodes: Appending newly created
media or animation nodes to an active
<par>or<seq>container causes the SMIL timing engine to calculate fresh instance times. - Pruning Active Nodes: Removing active children
dynamically cancels their scheduled end times and resolves remaining
container durations according to
endsyncrules.
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:
pauseAnimations(): Suspends playback across all active containers.unpauseAnimations(): Resumes playback from the current paused state.setCurrentTime(seconds): Fast-forwards or rewinds the document timeline to an arbitrary timestamp.getCurrentTime(): Retrieves the current elapsed time since document start.
Using these methods in tandem with DOM events gives JavaScript full real-time control over declarative SMIL media schedules.