ppedrazzi
ppedrazzi

Reputation: 787

Can't get Meteor.setInterval to work in methods

I am trying to use Meteor.setInterval to create a simple countdown timer (1 second at a time). I have all the click events working in the templates (verified by console.log). They trigger the methods, which I also verify work via console.log events in terminal.

I am trying to: - Start a countdown timer on #start click with Meteor.setInterval and an interval of 1000ms. - Pause a timer on #pause click by changing the existing intervalId to an interval of 0. - Cancel a timer on #cancel click by using Meteor.clearInterval(id).

I'd like to put each of these in my methods, but it does not seem to work. I can't seem to get the intervalId back and available for the other methods. I also am not sure where to put my interval function.

I have included my code without including the Meteor.setInterval or Meteor.clearInterval as I don't know where they should go.

Coffee code here:

if Meteor.isClient
    Meteor.startup () ->
        console.log "Client is Alive"
        Session.setDefault("timerStartValue", 25)
        Session.setDefault("timeRemaining", 25)

    Template.timer.helpers
        timeRemaining: () ->
            Session.get("timeRemaining")

        timerStartValue: () ->
            Session.get("timerStartValue")

    Template.timer.events
        "click #start": () ->
            console.log "Start button clicked."
            Meteor.call("start", (error, result) ->
                if error then console.log "Error is #{error}.")

        "click #pause": () ->
            console.log "Pause button clicked."
            Meteor.call("pause", (error, result) ->
                if error then console.log "Error is #{error}.")

        "click #cancel": () ->
            console.log "Cancel button clicked."
            Meteor.call("cancel", (error, result) ->
                if error then console.log "Error is #{error}.")


if Meteor.isServer
    Meteor.startup () ->
        console.log "Server is alive."

    Meteor.methods
        start: () ->
            console.log "started on server."

        pause: () ->
            console.log "paused on server."

        cancel: () ->
            console.log "cancelled on server."

Upvotes: 0

Views: 345

Answers (1)

ppedrazzi
ppedrazzi

Reputation: 787

I decided to build this outside of a method, keeping all code on the client. Seems to work fine. I included the code here in case anyone else might find it useful.

if Meteor.isClient
    Meteor.startup () ->
        console.log "Client is Alive"
        Session.setDefault("timerStartValue", 25)
        Session.setDefault("timeRemaining", 25)
        Session.setDefault("intervalId", 0)

    Template.timer.helpers
        timeRemaining: () ->
            Session.get("timeRemaining")

        timerStartValue: () ->
            Session.get("timerStartValue")

    Template.timer.events
        "click #start": () ->
            countDown = () ->
                t = Session.get("timeRemaining")
                if t > 0
                    Session.set("timeRemaining", t - 1)
                else
                    0
            intervalId = Meteor.setInterval(countDown, 1000)
            Session.set("intervalId", intervalId)
            console.log "Start button clicked."

        "click #pause": () ->
            Meteor.clearInterval(Session.get("intervalId"))
            console.log "Pause button clicked."

        "click #cancel": () ->
            Meteor.clearInterval(Session.get("intervalId"))
            Session.set("timeRemaining", Session.get("timerStartValue"))
            console.log "Cancel button clicked."

Upvotes: 1

Related Questions