简体   繁体   中英

Create synchronous countdown in Meteor

what I want to have a synched countdown in Meteor. Unfortunatly the value is not static. Let's say I have buttons for pause, rewind, add and substract in the backend.

My problem now is how to send the current state to other connected clients. I tried with collections like this but the collection isn't ready for atleast one tick. Meaning it get's executed every 2nd tick at best.

Template.frontend.helpers({
    clock() {
        return ClockSettings.find().fetch()[0];

    },
    runClock: (c) => {
        setInterval( () => {
            if(c) {
              adjustClock(c);
            }
        }, 1000);
    }
});

Maybe I built the start wrong but my assumption was to create something like a super variable which is synced across all clients without going other a collection. Or am I missing an even easier approach?

You should try an implementation with 'ReactiveVar'

https://docs.meteor.com/api/reactive-var.html

A possible implementation below:

let clock = new ReactiveVar()

Tracker.autorun(function(){
    clock.set(ClockSettings.findOne());
})

The performance of this will be greater then the above solutiion

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM