This tutorial is for:
By completing this tutorial, you will:
addWidget method to update existing widgetsremoveWidgetBefore starting this tutorial, ensure you have:
This tutorial uses the Live Match Tracker widget, but the rotation technique works with any widget type.
Widget rotation is useful for:
When you call addWidget with the same target element but different configuration (e.g., different matchId), the widget framework:
Create a simple page with a widget container and control button.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Widget Rotator</title>
<style>
.sr-widget {
width: 620px;
}
</style>
</head>
<body
Load the widget framework and configure your match IDs array.
(function(a,b,c,d,e,f,g,h,i){a[e]||(i=a[e]=function(){(a[e].q=a[e].q||[]).push(arguments)},i.
Replace the example match IDs with valid match IDs from your license. Using invalid IDs will result in widget errors.
Implement a function that adds or updates the widget with the next match ID.
function addOrUpdateWidget(callback) {
SIR('addWidget', '.sr-widget', 'match.lmtPlus', { matchId: matchIds[ix] }, callback);
// Move to next match ID, wrap to start if at end
if (++ix >= matchIds.length) { ix = 0; }
}How Widget Updating Works
addWidget - With the current matchId from the array.sr-widget already contains a widgetThe optional callback parameter allows you to perform actions after the widget loads, such as starting the rotation timer.
Load the first widget and start the automatic rotation timer.
// Load first widget, then start rotation
addOrUpdateWidget(function() {
// Start rotating every 3 seconds (3000ms)
interval = setInterval(addOrUpdateWidget, 3000);
});Change the interval duration to control rotation speed:
// Slower rotation - every 5 seconds
interval = setInterval(addOrUpdateWidget, 5000);
// Faster rotation - every 1 second
interval = setInterval(addOrUpdateWidget, 1000);Create a function to stop rotation and remove the widget from the page.
function endThisThing() {
// Stop the rotation timer
clearInterval(interval);
// Remove the widget from the page
SIR('removeWidget', '.sr-widget');
// Alternative: pass DOM element directly
// SIR('removeWidget', document.querySelector('.sr-widget'));
}SIR('removeWidget', '.sr-widget');Pass a CSS selector string. The framework will find and remove the widget.
View Complete Working Example
<!DOCTYPE html>
<html>
<head>
Add controls to pause and resume rotation:
let isPaused = false;
function toggleRotation() {
if (isPaused) {
// Resume rotation
interval = setInterval(addOrUpdateWidget, 3000);
isPaused = false;
} else {
// Pause rotation
clearInterval(interval);
isPaused = true;
}
}<button onclick="toggleRotation()">Pause/Resume</button>Add previous/next buttons for manual control:
function nextWidget() {
clearInterval(interval); // Stop auto-rotation
addOrUpdateWidget();
}
function previousWidget() {
clearInterval(interval); // Stop auto-rotation
// Move back 2 positions (current increment + desired back step)
ix = (ix - 2 + matchIds.length) % matchIds.length;
addOrUpdateWidget()
<button onclick="previousWidget()">← Previous</button>
<button onclick="nextWidget()">Next →</button>You can rotate between different widget types:
let widgets = [
{ type: 'match.lmtPlus', config: { matchId: 41960917 } },
{ type: 'match.scoreboard', config: { matchId: 41960918 } },
{ type: 'tournament.standings', config: { tournamentId: 12345 } }
];
Symptom: Brief blank screen between widget updates.
Cause: Widget needs time to fetch new data.
Solution: Increase rotation interval or add a loading state:
function addOrUpdateWidget(callback) {
// Show loading indicator
document.querySelector('.sr-widget').classList.add('loading');
SIR('addWidget', '.sr-widget', 'match.lmtPlus', {
matchId: matchIds[ix],
onTrack
Symptom: Console errors about missing elements after widget removal.
Cause: clearInterval wasn't called before removeWidget.
Solution: Always clear the interval first:
function endThisThing() {
// MUST clear interval before removing
clearInterval(interval);
SIR('removeWidget', '.sr-widget');
}Symptom: Widgets rotate faster or slower than expected.
Cause: Multiple intervals created without clearing previous ones.
Solution: Always clear existing interval before creating new one:
function startRotation(speed) {
// Clear any existing interval
if (interval) {
clearInterval(interval);
}
// Create new interval
interval = setInterval(addOrUpdateWidget, speed);
}Calling addWidget on an element that already contains a widget updates the existing widget rather than creating duplicates.
Always use clearInterval before removing widgets or starting new intervals to prevent memory leaks and errors.
The removeWidget method accepts either a CSS selector string or a DOM element reference for flexible widget removal.
removeWidget methodCalling addWidget multiple times on the same element updates the widget rather than creating duplicates.
Why use a callback? This ensures the first widget loads before starting the rotation timer, preventing rapid initial updates.
Always clear intervals before removing widgets to prevent memory leaks and errors from the interval trying to update a non-existent widget.