title: Meteor.js: How to periodically query collection documents from view
body: <p>I have a view that I want to be essentially a dynamic slideshow:</p>
  
  <ol>
  <li>load a view</li>
  <li>start a timer so that every X seconds a random document from a particular collection is returned (typical <code>collection.findOne()</code> request)</li>
  <li>when that item is loaded, trigger a function with the data attached</li>
  </ol>
  
  <p>this sounds like it could be a typical jquery _GET request, but it would be good to know the 'meteor' way of doing it.</p>
  
  <p>Here is essentially a starting point of what I'd like to do:</p>
  
  <pre class="lang-js prettyprint-override"><code>// week_player.js
  
  
  var randomWeekInterval;
  
  // triggered when page is first loaded
  Template.weekPlayer.rendered = function(){
      console.log('rendered!');
      var tick = 0;
      randomWeekInterval = Meteor.setInterval(function(){
  
          console.log('week interval update ' + (tick++));
  
          var weekCount = Weeks.find().count();
          var randomWeekId = Math.floor(weekCount * Math.random());
  
          var randomWeek = Weeks.findOne(randomWeekId);
  
          // TODO: add some sort of listener for when the week is loaded
          // and trigger newDataHandler with the data
  
      }, 10 * 1000); // repeat every 10 seconds
  }
  
  // triggered when the new data is loaded
  Template.weekPlayer.newDataHandler = function(data){
      // got new data! here it is:
      console.log(data);
  }
  
  // triggered when leaving page: stop doing the interval
  Template.postPage.destroyed = function(){
      Meteor.clearInterval(randomWeekInterval);
  }
  </code></pre>