修复 CoffeeScript 中的 setTimeout
Fixing setTimeout in CoffeeScript
我正在尝试将 this 代码片段从 JS 移植到 Coffee,但卡在忽略代码的设置超时函数中,根本没有添加 类 超时:
$ ->
$('.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
尝试提取函数,但这也无济于事。
好吧,问题似乎只出在 jQuery 代码上。
这对于您拥有的 html 来说似乎是错误的。
$span = $(this).closest('span')
改为
$span = $(this).children().first()
我正在尝试将 this 代码片段从 JS 移植到 Coffee,但卡在忽略代码的设置超时函数中,根本没有添加 类 超时:
$ ->
$('.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
尝试提取函数,但这也无济于事。
好吧,问题似乎只出在 jQuery 代码上。
这对于您拥有的 html 来说似乎是错误的。
$span = $(this).closest('span')
改为
$span = $(this).children().first()