The problem of getting the time stamp of the date and the inaccurate countdown of the front-end

The problem of getting the time stamp of the date and the inaccurate countdown of the front-end

1. The problem of getting the time stamp of the date: new Date(date).getTime(), using this method to get the time stamp, NaN is returned in ios, and the Android machine is normal.
Reason: date = '2020-09-09 12:00' In ios, time cannot be divided by'-', it should be divided by /, that is, “2020/09/09 12:00” should be used, otherwise the following error will be reported: Cannot assign to read only property'exports' of object ' #<Object>'

Therefore, before calling the new Date() method, you need to convert the format: date = date.replace(/-/g,'/')

 

2. The front-end countdown is not accurate

Countdown problem—use the user’s system time (ie new Date()) to implement the countdown problem: the user may adjust the time faster or slower

The time of the user's system. The time of the user's system is obtained with new Date(). This time can be adjusted faster.
If the user adjusts the time faster, it will cause problems with the countdown.

If you want to get the current time and compare it with another time, it is best to use the time returned by the server as a benchmark, so as to prevent the user from being inaccurate in time.

 

For example: the order creation time createdTime, the current time is curTime, diff = curTime-createdTime, when diff <1000, the countdown timer needs to be displayed, these two times are best returned to the front end in the background

Guess you like

Origin blog.csdn.net/tangxiujiang/article/details/110314293