等待按钮被点击

时间:2014-03-01 18:12:00

标签: javascript jquery asynchronous coffeescript

我想等到成功按下三个按钮之一后再运行下五个语句。我怎么能这样做?

playerTurn: (opponent) ->
    $attackButton.click -> buttonClick.attackButtonClick(opponent)

    $defendButton.click -> buttonClick.defendButtonClick(opponent)

    $useItemButton.click -> buttonClick.useItemButtonClick(opponent)

    status.Poison("opponent", opponent) if opponent.Poisoned
    status.Burn("opponent", opponent) if opponent.Burned
    status.Freeze("opponent", opponent) if opponent.Frozen
    opponent.undefend() if opponent.defenseDoubled or opponent.defenseTripled
    refresh(opponent)

1 个答案:

答案 0 :(得分:1)

您只需将语句包装在自己的函数中并为每次单击调用它吗?

playerTurn: (opponent) ->
  opponentAction = ->
    status.Poison "opponent", opponent  if opponent.Poisoned
    status.Burn "opponent", opponent  if opponent.Burned
    status.Freeze "opponent", opponent  if opponent.Frozen
    opponent.undefend()  if opponent.defenseDoubled or opponent.defenseTripled
    refresh opponent

  $attackButton.click ->
    opponentAction()
    buttonClick.attackButtonClick opponent

  $defendButton.click ->
    opponentAction()
    buttonClick.defendButtonClick opponent

  $useItemButton.click ->
    opponentAction()
    buttonClick.useItemButtonClick opponent