Skip to content

Instantly share code, notes, and snippets.

@davidworkman9
Last active December 24, 2015 07:49
Show Gist options
  • Save davidworkman9/6766259 to your computer and use it in GitHub Desktop.
Save davidworkman9/6766259 to your computer and use it in GitHub Desktop.
Extension of my random on interval demo
Template.randomImg1.img = function () {
return imgs.findOne({_id: Session.get('randomImg1') });
};
Template.randomImg2.img = function () {
return imgs.findOne({_id: Session.get('randomImg2') });
};
Template.randomImg3.img = function () {
return imgs.findOne({_id: Session.get('randomImg4') });
};
Template.randomImg4.img = function () {
return imgs.findOne({_id: Session.get('randomImg4') });
};
SetInterval(function () {
Meteor.methods('getRandomImg', function (err, _id) {
Meteor.subscribe('imgById', _id);
Session.set('randomImg2', _id);
});
}, 5 * 1000);
SetInterval(function () {
Meteor.methods('getRandomImg', function (err, _id) {
Meteor.subscribe('imgById', _id);
Session.set('randomImg3', _id);
});
}, 30 * 1000);
SetInterval(function () {
Meteor.methods('getRandomImg', function (err, _id) {
Meteor.subscribe('imgById', _id);
Session.set('randomImg4', _id);
});
}, 10 * 1000);
SetInterval(function () {
Meteor.methods('getRandomImg', function (err, _id) {
Meteor.subscribe('imgById', _id);
Session.set('randomImg1', _id);
});
}, 15 * 1000);
var randomWithinRange = function randomWithinRange (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
Meteor.methods({
'getRandomImg': function () {
var numToSkip = randomWithinRange(0, imgs.find().count());
return imgs.findOne({}, {limit: 1, skip: numToSkip})._id;
}
});
Meteor.publish('imgById', function (_id) {
check(_id, Number);
return imgs.find({_id: _id});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment