简体   繁体   中英

html/javascript. Why does my code trigger audio slowly?

i hope my code trigger the audio when user just press the key on keyboard. At first time? it's Okay.
but if i frequently pressed some key, the audio was triggered slowly.

This is my html code, loading the audio file.

<audio src = "./src/PMLF_Snare_02.wav" id = "audio"></audio> 

and this is my javascript code which triggers audio file.

$(function(){
    var audio = document.getElementById('audio');

    // Key Down     
    window.addEventListener('keydown', function(){
        //padding effect
        $("#"+event.key).css('padding',' 4px 5px 4px 5px');
        
        //play sound
        audio.pause();
        audio.currentTime = 0;
        audio.play();       
    });
 
    
    '  
    ' 
    (skip)
    '
    '

});

does my code load audio file whenever key is pressed? how can i get the fast reaction sound like this site ?

I hope this helps, you can try this code, is vanilla JS but is working, the audio plays when you press Enter keyword (keycode 13). Good luck with your proyect!

 var audio = document.getElementById('audio'); window.addEventListener("keydown", function(event) { if (event.keyCode === 13) { audio.play(); }; });

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