Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How about a different approach: who cares about time? (You're not going to reliably sync the system clock with JavaScript.)</p> <p>Instead, use a <a href="http://nodejs.org" rel="noreferrer">Node</a> server with <a href="http://socket.io" rel="noreferrer">socket.io</a> to synchronize when your clients advance the slideshow. Instead of the clients deciding when to advance, the server tells them to.</p> <p>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 <em>Next</em> button that causes all connected clients to immediately advance to the next slide.</p> <h1>app.js</h1> <pre><code>var express = require('express') , app = express.createServer() , io = require('socket.io').listen(app) , doT = require('dot') , slide = 0 , slides = [ 'http://placekitten.com/700/400?image=13', 'http://placekitten.com/700/400?image=14', 'http://placekitten.com/700/400?image=15', 'http://placekitten.com/700/400?image=16', 'http://placekitten.com/700/400?image=1', 'http://placekitten.com/700/400?image=2', 'http://placekitten.com/700/400?image=3', 'http://placekitten.com/700/400?image=4', 'http://placekitten.com/700/400?image=5', 'http://placekitten.com/700/400?image=6', 'http://placekitten.com/700/400?image=7', 'http://placekitten.com/700/400?image=8', 'http://placekitten.com/700/400?image=9', 'http://placekitten.com/700/400?image=10', 'http://placekitten.com/700/400?image=11', 'http://placekitten.com/700/400?image=12', ]; app.listen(70); // listen on port 70 app.register('.html', doT); // use doT to render templates app.set('view options', {layout:false}); // keep it simple doT.templateSettings.strip=false; // don't strip line endings from template file app.get('/', function(req, res) { res.render('index.html', { slide: slide, slides: slides }); }); app.post('/next', function(req, res) { next(); res.send(204); // No Content }); setInterval(next, 4000); // advance slides every 4 seconds function next() { if (++slide &gt;= slides.length) slide = 0; io.sockets.emit('slide', slide); } </code></pre> <h1>views/index.html</h1> <p>This file is processed as a <a href="http://olado.github.com/doT/" rel="noreferrer">doT</a> template.</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Synchronized Slideshow&lt;/title&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="/socket.io/socket.io.js"&gt;&lt;/script&gt; &lt;script&gt; var curslide = {{=it.slide}}; // the slide the server is currently on. $(function() { $('#slide' + curslide).css('left',0); $('#next').click(function() { $.post('/next'); }); }); var socket = io.connect('http://localhost:70'); socket.on('slide', function(slide) { $('#slide' + curslide).animate({left:-700}, 400); $('#slide' + slide).css('left',700).show().animate({left:0}, 400); curslide = slide; }); &lt;/script&gt; &lt;style&gt; #slideshow, .slide { width:700px; height:400px; overflow:hidden; position:relative; } .slide { position:absolute; top:0px; left:700px; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="slideshow"&gt; {{~it.slides :url:i}} &lt;div id="slide{{=i}}" class="slide"&gt;&lt;img src="{{=url}}"&gt;&lt;/div&gt; {{~}} &lt;/div&gt; &lt;button id="next"&gt;Next &amp;gt;&lt;/button&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Copy these two files into a folder, then run</p> <pre><code>$ npm install express socket.io dot $ node app.js </code></pre> <p>and navigate to <code>http://localhost:70</code> in several different windows, then see the magic.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload