Rails Backbone Coffeescript清除功能间隔

时间:2014-07-22 21:26:37

标签: ruby-on-rails backbone.js coffeescript

如何从我当前的功能中清除所有间隔" hp_added()"?或者更好,如何防止此功能调用两次?我几乎总是需要调用它,有一次,但它被许多其他函数调用,并且每次都会增加计数。无法在互联网上找到明确的解决方案。

  window.delay = (ms, fn)-> setTimeout(fn, ms)
  window.timer = (ms, fn)-> setInterval(fn, ms)

  hp_added: ->
    if @model.attributes.player_hp < @model.attributes.max_hp
      @hp_adding()

  hp_adding: ->
    self = this
    if Interval?
      clearInterval this.hp_adding()   //its not working
    timer 2000, ()-> self.model.set(player_hp: self.model.attributes.player_hp+1) and self.model.save() if self.model.attributes.player_hp < self.model.attributes.max_hp

我为我找到了一个解决方案,它始终工作,我只需要在开始时调用它一次。也许somoene知道更好的解决方案?

  initialize: ->
    @hp_ad()

  hp_ad: ->
    alfas = "true"
    if alfas is "true"
      @hp_added()
      alfas = "false"

  hp_added: ->
    self = this
    timer 3000, ()-> self.hp_adding()

  hp_adding: ->
    if @model.attributes.player_hp < @model.attributes.max_hp
      @model.set(player_hp: @model.attributes.player_hp+1)
      @model.save()

0 个答案:

没有答案