Skip to content

Commit b0d00fe

Browse files
committed
"Add functionality to pause audio 5 seconds before the end"
1 parent 9d8a0a2 commit b0d00fe

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

script.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ function nextQuestion() {
7373
// Load and play the audio
7474
const audioElement = document.getElementById('question-audio');
7575
audioElement.src = `audio/KNS ${currentQuestionId.padStart(3, '0')}.mp3`;
76+
77+
// Remove any existing timeupdate event listeners
78+
audioElement.removeEventListener('timeupdate', pauseBeforeEnd);
79+
80+
// Add event listener to pause 5 seconds before the end
81+
audioElement.addEventListener('timeupdate', pauseBeforeEnd);
82+
7683
audioElement.load();
7784
audioElement.play().catch(error => {
7885
console.error('Error playing audio:', error);
@@ -155,3 +162,19 @@ function handleImageError() {
155162

156163
// Add error handler to image
157164
document.getElementById('question-image').addEventListener('error', handleImageError);
165+
166+
// Function to pause audio 5 seconds before the end
167+
function pauseBeforeEnd() {
168+
const audioElement = document.getElementById('question-audio');
169+
170+
// Check if the audio is 5 seconds from the end
171+
if (audioElement.duration > 0 && audioElement.currentTime > 0 &&
172+
(audioElement.duration - audioElement.currentTime) <= 5) {
173+
174+
// Pause the audio
175+
audioElement.pause();
176+
177+
// Remove the event listener to prevent multiple pauses
178+
audioElement.removeEventListener('timeupdate', pauseBeforeEnd);
179+
}
180+
}

0 commit comments

Comments
 (0)