2014-11-29 8 views
0

Я пытаюсь использовать Meteor.setInterval для создания простого таймера обратного отсчета (1 секунда за раз). У меня есть все события кликов, которые работают в шаблонах (проверено console.log). Они запускают методы, которые также проверяют работу через события console.log в терминале.Невозможно получить Meteor.setInterval для работы в методах

Я пытаюсь: - Запустить таймер обратного отсчета на #start с помощью Meteor.setInterval и интервала в 1000 мс. - Приостановить таймер на #pause, нажав, изменив существующий интервалId на интервал 0. - Отмените таймер на #cancel, нажав кнопку 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." 

ответ

1

я решил построить это вне способа, сохраняя весь код на клиенте. Кажется, хорошо работает. Я включил здесь код, если кто-то может найти его полезным.

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." 
Смежные вопросы