Help fix animation flip timer
Hello everyone,
I'm working on a project and I'm having trouble finding a solution.
My project is to make an animation of flipping through a page of a book on a Timer (flip timer).
To test my animation, I tested it on the seconds.
The problem is that the animation isn't synchronized with the change of second, so it runs twice as fast.
Here's my code: https://codepen.io/shikamaru098/pen/ZEdvpex
8 Replies
You are currently initiating the "flip" animation and the counter at two different moments in the code. Whilst JS is fast, that may be causing an issue.
You could try moving the animate() function inside of the setInterval code 🤔.
I would also look at moving the variables that don't change outside of the setInterval function so that the JS doesn't have to process them again every second. This should also save time and reduce any difference.
For example, based on your current code, you could do something like this:
You are also updating all of the DOM for every number every time, even though they haven't changed. Again, this is creating unnecessary processing for the JS.
I would consider only updating the numbers that have changed.
[update] I should add that I am not actually seeing any lag or different timing on your code so, either you have already fixed the issue or it is browser/OS dependent.
Another thing that you could do is update the time as soon as the page loads as currently you have to wait a second for the time to make its first change.
I have forked your pen and made a few changes that I have suggested:
https://codepen.io/cbolson/pen/VwJRxPE
Take a look and see if they resolve the issue and if what I changed makes any sense.
The results look functionally the same. If we're all seeing the same thing, the problem is in the design, not the details - the number changes before the "card" flips, breaking the illusion of it being a physical, printed object. The previous value needs to flip.
I think that that is a separate issue. Either that or I miss-understood what the actual problem was.
As the OP mentioned it running "twice as fast" I took that to mean that the animation was running faster than the timer so I was concentrating on that issue (admittedly without really getting anywhere as I couldn't reproduce the problem).
I must admit that I didn't look too closely at the actual animation itself though clearly that is something that would need to be fixed too.
Maybe Rochelin could share a quick video of the issue as they see it to see if we are all seeing the same thing.
It's exactly that in the design: the number changes before the card is turned over, which breaks the illusion.
In fact, my problem is what myssiemae mentioned in the design: the number change before the card flips
OK, so has nothing to do with the speed. As in your original post you said that "it runs twice as fast" so I took that to mean that the issue was with the speed.
Ok but it's possible to fix it?
I think that you will need to delay some of the number changing both on the “flip” numbers and the main numbers.
You could also speed up the animation so that it takes less than a second to give you time to see the numbers and less time to see the number swapping.
I am working on some changes to my fork of your code but have had to pop out.
You can take a look but for now I have only looked at the right-hand number.
ah, I have just realized that you had only added the flip to the last number. I thought that I had broken my version 🤪
I think that the timing is pretty good now so I will leave it, at least for now, so you can continue with yours, using my timing adjustments as you feel fit.
[update] - the key thing that I was suggesting was to animate the different parts of the number with a slight delay on some parts:
whilst at the same time speeding up the animation itself to 500ms rather than 1000ms.