javascript 手风琴 - 跟踪时间问题

发布于 2024-09-04 04:15:28 字数 703 浏览 6 评论 0原文

我正在阅读这个 javascript 教程: http://www.switchonthecode.com/tutor...ccordion-menus 基本上,它向您展示了如何使用纯 javascript 而不是 jquery 创建手风琴。在跟踪动画的实际部分之前,所有这些对我来说都是有意义的。他说:“正因为如此,我们在动画函数中做的第一件事就是计算出自上次动画迭代以来已经过去了多少时间。” 然后使用这段代码: 代码:

var elapsedTicks = curTick - lastTick;

lastTick 等于调用函数时的值(Date().getTime()),curTick 等于接收到函数时的值。我不明白为什么我们要在这里减去一个。我无法想象这两个值之间有任何明显的时间差异。或者也许我错过了一些东西。 animate() 函数是仅在每次单击菜单标题时调用一次,还是调用多次以创建增量动画效果?

setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ",'" + openAccordion + "','" + nID + "')", 33); 

感谢您的任何回复。

I was reading up on this javascript tutorial:
http://www.switchonthecode.com/tutor...ccordion-menus
Basically, it shows you how to create an accordion using pure javascript, not jquery. All made sense to me until the actual part of tracking the animation. He says "Because of all that, the first thing we do in the animation function is figure out how much time has passed since the last animation iteration."
And then uses this code:
Code:

var elapsedTicks = curTick - lastTick;

lastTick is equal to the value of when the function was called (Date().getTime()) and curTick is equal to the value when the function was received. I don't understand why we are subtracting one from the other right here. I can't imagine that there's any noticeable time difference between these two values. Or maybe I'm missing something. Is that animate() function only called once every time a menu title is clicked or is it called several times to create the incremental animation effect?

setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ",'" + openAccordion + "','" + nID + "')", 33); 

Thanks for any response.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓦然回首 2024-09-11 04:15:28

lastTick 等于调用函数时的值

lastTick 等于在动画的最后一帧先前调用该函数时的值。此后,脚本将控制权交还给浏览器,并要求在 33 毫秒内回调。

因此,curTick-lastTick 通常大约 33,但如果浏览器由于同时发生其他事情而出现延迟,则该值可能会更高。这就是为什么必须进行时间阅读的原因。

更常见的是,在此类代码中,您会将动画开始的时间存储在变量中,并使用setInterval经常检查它,而不是设置完整的时间每次都有新的超时函数(特别是从字符串设置超时,这是超级丑陋的)。

预计到达时间:

然后运行 ​​animate() 函数,该函数传递当前时间

。再看一下 set-timeout 调用:

setTimeout("animate(" + new Date().getTime() + ","...

这正在创建一个字符串。 new Date().getTime() 在超时设置时间评估,而不在超时调用时间评估。它最终会生成一个如下字符串:

setTimeout("animate(1275139344177, 250, 'Accordion4Content', 'Accordion4Content')", 33)

这是最后一帧结束时的时间,而不是下一帧超时的时间。

像这样将 JavaScript 代码放入字符串中是非常令人困惑的,并且充满了转义问题,并且通常被认为是非常糟糕的做法。使用内联函数会更清楚:

var passTick= new Date().getTime();
setTimeout(function() {
    animate(passTick, TimeToSlide, openAccordion, nID);
}, 33);

lastTick is equal to the value of when the function was called

lastTick is equal to the value when the function was previously called, on the last frame of animation. Since then, the script has given control back to the browser, asking to be called back in 33 milliseconds.

So curTick-lastTick will generally be about 33, but it could be much higher if the browser is lagged due to other stuff happening at the same time. This is why time-reading has to be done at all.

More usually in this sort of code, you'd store the time the animation started in a variable, and use setInterval to check it every so often, instead of setting a complete new timeout function each time (especially setting a timeout from a string, which is super-ugly).

eta:

then runs the animate() function, which passes the current time

Nope. Look at the set-timeout call again:

setTimeout("animate(" + new Date().getTime() + ","...

That's making a string. new Date().getTime() is evaluated at timeout-setting time, not at timeout-calling time. It ends up making a string like:

setTimeout("animate(1275139344177, 250, 'Accordion4Content', 'Accordion4Content')", 33)

Which is the time at the end of the last frame, not the time the next frame's timeout will fire.

Putting JavaScript code in a string like this is super-confusing, rife with escaping problems, and generally regarded as really poor practice. It would be clearer to do it with an inline function:

var passTick= new Date().getTime();
setTimeout(function() {
    animate(passTick, TimeToSlide, openAccordion, nID);
}, 33);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文