Methods Can';t让Meteor.setInterval在方法中工作

Methods Can';t让Meteor.setInterval在方法中工作,methods,timer,meteor,coffeescript,Methods,Timer,Meteor,Coffeescript,我正在尝试使用Meteor.setInterval创建一个简单的倒计时计时器(每次1秒)。我的所有点击事件都在模板中工作(由console.log验证)。它们触发这些方法,我还通过终端中的console.log事件验证这些方法的工作 我正在努力: -使用Meteor.setInterval在#开始单击上启动倒计时,间隔为1000毫秒。 -暂停计时器#通过将现有有效期更改为0的间隔来暂停单击。 -取消计时器#使用Meteor.clearInterval(id)取消单击 我想在我的方法中加入每一项,

我正在尝试使用Meteor.setInterval创建一个简单的倒计时计时器(每次1秒)。我的所有点击事件都在模板中工作(由console.log验证)。它们触发这些方法,我还通过终端中的console.log事件验证这些方法的工作

我正在努力: -使用Meteor.setInterval在#开始单击上启动倒计时,间隔为1000毫秒。 -暂停计时器#通过将现有有效期更改为0的间隔来暂停单击。 -取消计时器#使用Meteor.clearInterval(id)取消单击

我想在我的方法中加入每一项,但它似乎不起作用。我似乎无法恢复intervalId并可用于其他方法。我也不确定我的区间函数放在哪里

我已经包括了我的代码,但没有包括Meteor.setInterval或Meteor.clearInterval,因为我不知道它们应该放在哪里

咖啡代码如下:

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."

我决定在方法之外构建它,将所有代码保留在客户机上。看起来很好用。我在这里包含了代码,以防其他人发现它有用

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."