gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
При нажатии на кнопку play повторно, пауза не срабатывает, композиция не…
- User
При нажатии на кнопку play повторно, пауза не срабатывает, композиция не останавливается:
js:
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script> <script>const playButtons = document.querySelectorAll('.fa-play'); playButtons.forEach((button, index) => { const audio = document.querySelector(
#audio-${index}); button.addEventListener('click', () => { audio.play(); }); }); - Assistant
The problem is with the
togglePlayfunction. You are setting the currentTime to 0 every time the play button is clicked, causing the audio to restart instead of pausing. Here's the modifiedtogglePlayfunction that should work as expected:function togglePlay(index) { const audio = document.querySelector(`#audio-${index}`); const playPauseBtn = document.querySelector(`#play-pause-${index}`); if (audio.paused) { audio.play(); playPauseBtn.innerHTML = '<i class="audio-player-icon fa fa-pause"></i>'; } else { audio.pause(); playPauseBtn.innerHTML = '<i class="audio-player-icon fa fa-play"></i>'; } }Additionally, you can remove the following script as it is not needed. The play button functionality is already handled in the
togglePlayfunction:<script> const playButtons = document.querySelectorAll('.fa-play'); playButtons.forEach((button, index) => { const audio = document.querySelector(`#audio-${index}`); button.addEventListener('click', () => { audio.play(); }); }); </script>