samedi 3 janvier 2015

Unwanted Delay in Playing Audio on-click

I have a webpage that shows pictures. Upon clicking on a picture, I would like it to play some sound file (an audio recording of the name of the object in the picture). I have working code for this, but there's a noticeable delay between clicking on the image, and hearing the audio file being played. How can I improve my code to get rid of that delay?


HTML:



<div>
<button style="background-image:url('objects/images/horlicks.JPG'); width:480; height:640" onclick="changeText('horlicks', 'Horlicks', 'objects/sounds/horlicks.mp3')">
</button>
<p id="horlicks"></p>
</div>


JavaScript:



function changeText(id, newText, audioFile) {
document.getElementById(id).innerHTML = newText; // This happens quickly
new Audio(audioFile).play(); // This takes a long time
}

Aucun commentaire:

Enregistrer un commentaire