Search code examples
javascriptjquerycoffeescript

Fixing setTimeout in CoffeeScript


I am trying to port this code snippet to Coffee from JS, but getting stuck at the set timeout function which ignores the code, no classes are getting added on timeout at all:

$ ->

  $('.click').click ->
    $span = $(this).closest('span')
    $this = $(this)

    if $span.hasClass('fa-star')

      $this.removeClass 'active'

      setTimeout (->
        $this.removeClass 'active-2'
      ), 30

      $this.removeClass 'active-3'

      setTimeout (->
        $span.removeClass 'fa-star'
        $span.addClass 'fa-star-o'
      ), 15

    else

      $this.addClass 'active'
      $this.addClass 'active-2'

      setTimeout (->
        $span.addClass 'fa-star'
        $span.removeClass 'fa-star-o'
      ), 150

      setTimeout (->
        $this.addClass 'active-3'
      ), 150

Tried extracting the function but that didn't help either.


Solution

  • Well the issue seems to be just with the jQuery code.

    This looks wrong for the html you have.

    $span = $(this).closest('span')
    

    Change it to

    $span = $(this).children().first()
    

    DEMO