我写了一个有用的库来绘制和动画化SVG路径:segment。你可以在github上查看。
一段时间以前,我(接受了一个Pull Request)用requestAnimationFrame
替换了setTimeout
调用,以加快现代浏览器中的动画速度,还使用了Paul Irish提供的polyfill。
现在,当我尝试在多个paths
之间设置非常低的延迟时,我正在尝试使用requestAnimationFrame
调用的错误行为。
我准备了两个演示文稿,展示了setTimeout
(正常工作)和requestAnimationFrame
(错误行为)的行为。请检查它:
在requestAnimationFrame
演示中,我对我的库进行了一些修改,以便在控制台打印出一些有用的信息,您可以在其中看到错误行为:
(function calc(){
// Checking if it's the first element, the buggy behavior happens in the firsts elements
if(that.class === 'first'){
console.log('calc');
}
// Some code here that can break the recursive loop and stop execution of calc function
if(that.class === 'first'){
console.log('calc call');
}
that.timer = window.requestAnimationFrame(calc);
// More code here
})();
根据先前的代码,每次出现“calc call”消息后都应该出现一个“calc”消息。但至少在Firefox和Chrome中,我看不到这种情况。这是大多数时候的控制台输出:
calc
calc call
calc
calc call
我真的不知道发生了什么事情,所以任何帮助都是受欢迎的 :)
calc
函数时,另一个“call”消息也必须出现(在calc
函数的第一句话)。最后的控制台输出不能是“call calc”,而必须是“calc”,因为唯一停止循环的方法是放在写有// Some code here
的地方。希望现在清楚了,并且抱歉回答的延迟。 - lmgonzalvesreturn
。如果没有再次调用requestAnimationFrame()
,那么循环将在那里停止。 - Paul LeBeauif
条件退出了循环,这不是问题。问题在于我调用了window.requestAnimationFrame(calc);
,而在第二次调用中它没有运行calc
函数,因为 'calc' 消息没有显示。 - lmgonzalves