简体   繁体   English

使用requestAnimationFrame的Buggy行为

[英]Buggy behavior using requestAnimationFrame

I've wrote a useful library to draw and animate the stroke of SVG paths: segment. 我写了一个有用的库来绘制和动画SVG路径的笔画:段。 You can check it on github . 你可以在github上查看它

Some time ago I've replaced (accepting a Pull Request) the setTimeout calls with requestAnimationFrame , to speed up the animation in modern browsers, also using the polyfill provided by Paul Irish. 前段时间我用requestAnimationFrame替换了(接受Pull Request) setTimeout调用,以加速现代浏览器中的动画,同时使用Paul Irish提供的polyfill

Now I'm experimenting a buggy behavior with requestAnimationFrame calls when I tried to animate multiple paths with a very low delay among them. 现在,当我尝试使用延迟非常低的多个paths设置动画时,我正在尝试使用requestAnimationFrame调用的错误行为。

I've prepared 2 demos to show the behavior with both setTimeout (working properly) and requestAnimationFrame (buggy behavior). 我准备了两个演示来显示setTimeout (正常工作)和requestAnimationFrame (buggy行为)的行为。 Check it please: 请检查一下:

In the requestAnimationFrame Demo, I've modified a bit my library to print in the console some useful info, where you can see the buggy behavior: requestAnimationFrame Demo中,我修改了一下我的库,在控制台中打印一些有用的信息,在那里你可以看到bug的行为:

(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
})();

According to the previous code, after every 'calc call' message should appear a 'calc' message. 根据前面的代码,在每个'calc call'消息之后应该出现'calc'消息。 But that is not what I can see, at least in Firefox and Chrome. 但这不是我能看到的,至少在Firefox和Chrome中是这样。 This is the console output most of times: 这是控制台输出的大多数时间:

calc
calc call
calc
calc call

I really have no idea what's going on, so any help is welcome :) 我真的不知道发生了什么,所以欢迎任何帮助:)

The buggy behavior here is being caused because both timeoutID and return value from requestAnimationFrame are being saved into the same variable. 这里的错误行为是由于来自requestAnimationFrame timeoutID和返回值都被保存到同一个变量中而引起的。 Saving them in different variables will fix the issue. 将它们保存在不同的变量中将解决问题。

Here is a codepen 这是一个codepen

http://codepen.io/anon/pen/KzPboY?editors=0010 http://codepen.io/anon/pen/KzPboY?editors=0010

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM