2014-11-29 2 views
0

Meteor.setInterval을 사용하여 간단한 카운트 다운 타이머 (한 번에 1 초)를 만들려고합니다. 템플릿 (console.log에 의해 확인 됨)에서 모든 클릭 이벤트가 작동합니다. 그들은 메소드를 트리거하고 터미널의 console.log 이벤트를 통해 작업을 검증합니다.메서드에서 작동하도록 Meteor.setInterval을 가져올 수 없습니다.

다음을 시도합니다. - #start 클릭시 Meteor.setInterval 및 1000ms 간격으로 카운트 다운 타이머를 시작하십시오. - #pause에서 타이머 일시 중지 기존 intervalId를 0 간격으로 변경하여 클릭하십시오.- Meteor.clearInterval (id)을 사용하여 #cancel 클릭시 타이머를 취소하십시오.

나는 각각을 내 방법에 넣고 싶지만 작동하지 않는 것 같습니다. 나는 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." 
관련 문제