Better way of getting time in milliseconds in javascript?

前端 未结 5 1204
说谎
说谎 2021-01-30 05:06

Is there an alternative in JavaScript of getting time in milliseconds using the date object, or at least a way to reuse that object, without having to instantiate a new object e

5条回答
  •  逝去的感伤
    2021-01-30 05:30

    As far that I know you only can get time with Date.

    Date.now is the solution but is not available everywhere : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/now.

    var currentTime = +new Date();
    

    This gives you the current time in milliseconds.

    For your jumps. If you compute interpolations correctly according to the delta frame time and you don't have some rounding number error, I bet for the garbage collector (GC).

    If there is a lot of created temporary object in your loop, garbage collection has to lock the thread to make some cleanup and memory re-organization.

    With Chrome you can see how much time the GC is spending in the Timeline panel.

    EDIT: Since my answer, Date.now() should be considered as the best option as it is supported everywhere and on IE >= 9.

提交回复
热议问题