I’m using the wonderful reveal.js library to create a HTML slideshow. My only problem is that I need it to synchronise across multiple devices.
At the moment I am making a AJAX request to the the time from the server and keep an internal clock for the page.
function syncTime() {
// Set up our time object, synced by the HTTP DATE header
// Fetch the page over JS to get just the headers
console.log("syncing time")
var r = new XMLHttpRequest();
r.open('HEAD', document.location, false);
r.send(null);
var timestring = r.getResponseHeader("DATE");
systemtime = new Date(timestring); // Set the time to the date sent from the server
}
Whilst this gets me within 1 or so seconds of accuracy, I need to do better. The difference is really noticeable when the slideshow is auto advancing.
The code is going to be running all on the same platform, so cross-browser compatibility is nothing to worry about.
Here’s what I’ve managed to put together
Any ideas?
How about a different approach: who cares about time? (You’re not going to reliably sync the system clock with JavaScript.)
Instead, use a Node server with socket.io to synchronize when your clients advance the slideshow. Instead of the clients deciding when to advance, the server tells them to.
This approach comes with the added bonus of being able to manually fiddle with the slideshow while it’s running. In the example that follows, I’ve added a Next button that causes all connected clients to immediately advance to the next slide.
app.js
views/index.html
This file is processed as a doT template.
Copy these two files into a folder, then run
and navigate to
http://localhost:70in several different windows, then see the magic.