Home > Web Front-end > JS Tutorial > body text

A brief discussion on the stopping conditions of uniform motion in Javascript_Basic knowledge

WBOY
Release: 2016-05-16 16:25:37
Original
1427 people have browsed it

Let’s first take a look at the previous uniform motion code and see what kind of bug will occur after modifying the speed. Two benchmarks are added here for testing

Copy code The code is as follows:


       
   
   
          
           
          

                                                                                                  

                                                                                                  

                                                                                       



In fact, if the speed of such a code is changed to an odd number like 7, but the arrival target point is an integer, then there will be a bug that fails to reach the target point or jitters back and forth beyond the target point

Then why does this happen?

In fact, when he reaches the target point, he cannot accurately reach the target point. If the target point is 100, and he walks 7 times each time, he will either pass the target point at this time, or he will not pass it.

Never reach the target point. In fact, it is a bit similar to the previous buffer.

So how do you calculate that you have reached the target point?

For example: When you take a taxi to a certain place, the driver must have stopped about 10 or 20 meters away from where you are, then you have arrived. It's impossible to ask the car to stop at that place.

So, in fact, the procedure is the same. As long as the distance between the object and the target point is close to a certain level, there is no need to get closer, and we think it is done.

Let’s take a look at the modified code:

Copy code The code is as follows:
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template