好像我尝试这种方式的每种方式都会引发某种错误。以下是我的代码现在的样子:CoffeeScript中的Backbone.js setTimeout()循环
runShow: ->
moments = @model.get('moment_stack_items')
if inc == moments.length
inc = 1
pre = 0
$("#" + moments[pre].uid).hide("slide", { direction: "left" }, 1000)
$("#" + moments[inc].uid).show("slide", { direction: "right" }, 1000)
inc += 1
pre += 1
console.log "looping" + inc
t = setTimeout(this.runShow(),2000);
我在我的事件中调用该函数。 我有inc = 1
和pre = 0
定义的Backbone.View外。我当前的错误是“未捕获TypeError:对象[对象DOMWindow]没有方法'runShow'”
奖金要点:如何从另一个函数引用t我的clearTimeout(t))?
使用setTimeout中的字符串在幕后调用eval。我会强烈考虑将其改为该函数的参考。例如t = setTimeout(this.runShow,2000); http://stackoverflow.com/questions/86513/why-is-using-javascript-eval-function-a-bad-idea – Gazler
够公平的@Gazler,我已经从代码中删除它。 user576875解决方案也将其删除。 – thatmiddleway