简体   繁体   中英

How can i get the newest currentTime in wavesurfer.js?

I have problem. This code adds a click event to the wavebar.

wavesurfer.drawer.on("click", () => {console.log(wavesurfer.getCurrentTime())})

When i click on the wave bar, i get the time where the cursor was in! For example if the current time is 0 and i click on the wave bar and where i clicked on the wave bar is 3:03, i see that the log is 0! But i want to get the latest time. In this example the latest time is 3:03 but 0 will be logged as the current time. an other example is when the current time is 3:00, and i move the cursor on where the time is 4:04, 3:00 will be logged and i dont want it. I want the time of where i moved the cursor. I want 3:03 in example 1 and 4:04 in example 2. How can i solve this problem? Thanks for helping.

i fixed my problem. i wanted to get the time of where i clicked on. the event handler runs before the time gets update. and in this situation, i should have a delay to get the time of where i clicked. so i should use setTimeout method.

wavesurfer.drawer.on("click", (e) => {
  setTimeout(() => {
    dispatch(musicSlice.updateMusicTime(wavesurfer.getCurrentTime()));
  }, 0);
});

if you want to know why i gave setTimeout 0 second of delay, please do a research about call stack , web apis and asynchronous in js and you'll understand why i gave 0 second of delay to setTimeout method. i hope it helps you. good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM