Queue() / dequeue() JQuery工作不正常
Queue() / dequeue() JQuery not working properly
我一直在使用CoffeScript和JQuery。我需要创建一个简单的动画。
我试着使用delay,但我根本不理解它,所以我之前读了一个问题:
本页链接
所以我决定使用队列;主要的问题是,虽然队列中有更多的元素,但只考虑动画的最后一个元素。
我希望你能帮助我:),你可以找到下面的代码。
队列中的所有元素(CoffeScript)。
for proc in listaDeProcesos
id = proc.id
nombre = proc.nombre
idFinal = "#{id}#{nombre}"
tiempo = parseInt(proc.tejecucion)
$("#animacion").queue('chain',(next)->
$("##{idFinal}").fadeOut(tiempo,->
next()
)
)
dequeue函数调用:
$("#animacion").dequeue('chain')
恭喜!对所有JavaScript(或CoffeeScript)开发人员来说,这是一个必经之路。你最终一定会遇到的。原因有两方面:
- JavaScript(和CoffeeScript)变量是函数作用域,而不是块作用域 关闭
基本上,每次迭代创建一个函数,其中引用变量 idFinal
,而不是在该循环迭代中引用idFinal
的值。所以当以后的迭代改变idFinal
时,它会改变你所创建的所有函数。这里有一个更简单的行为示例:
JavaScript闭包内循环-简单的实际例子
在CoffeeScript中解决这个问题的一种方法是使用do
构造引入一个新的作用域:
for {id, nombre, tejecucion} in listaDeProcesos
do (idFinal = "#{id}#{nombre}", tiempo = parseInt(tejecucion)) ->
$("#animacion").queue 'chain', (next) ->
$("##{idFinal}").fadeOut tiempo, next
我找到了另一种解决方案,以防别人有同样的问题,我写下面的代码,谢谢:)
所有的代码都在coffeescript中
动画元素类
class animatedElement
constructor: (@element,@time) ->
showInfo: ->
alert ("#{@element} >> #{@time}")
你想要动画的迭代:
for proc in listaDeProcesos
id = proc.id
nombre = proc.nombre
idFinal = "#{id}#{nombre}"
tiempo = parseInt(proc.tejecucion)
elemento = new animatedElement($("##{idFinal}"),tiempo)
queue.push(elemento)
配套功能showAnimation = ()->
if (queue.length > 0)
tiempo = queue[0].time
(queue.shift().element).fadeOut(tiempo, showAnimation)
最后你只需要调用
showAnimation()
我希望它能对别人有用。
相关文章:
- Javascript.getHours()工作不正常
- removeClass函数没有'工作不正常
- jQuery Ajax GET请求工作不正常
- Cordova ng路线工作不正常
- 更新属性工作不正常Meteor/MongoDB
- 为什么文本对齐:对;工作不正常
- Javascript作用域和Ajax调用;工作不正常
- JavaScript DOM insertBefore'工作不正常
- Angular Js布线工作不正常
- onmouseout函数工作不正常
- 承诺工作不正常
- JQuery.live()工作不正常
- Alloy UI文本区域工作不正常
- ng src工作不正常,但src工作正常
- Canvas.toDataUrl(“image/png”)工作不正常
- JQuery `length`属性工作不正常.为什么?
- Javascript setInterval工作不正常
- 我的Javascript代码没有'工作不正常
- 表单验证工作不正常,在不检查条目的情况下继续
- $localStorage array select with ng选项工作不正常