javascript - How to continue playing sound when holding key? -
javascript - How to continue playing sound when holding key? -
i have sound in javascript plays when press arrow key. have sound play when hold arrow key, pause when release key.
this code now:
var clicksound = new audio("img/hartslag.mp3"); document.body.onkeyup = function (e){ if(e.keycode == 38){ clicksound.play(); } }
you need hear 2 different events. play on key downwards , pause on key up.
var clicksound = new audio("img/hartslag.mp3"); var playing = false; document.body.onkeydown = function (e){ if(e.keycode == 38 && !playing){ clicksound.play(); playing=true; } } document.body.onkeyup = function (e){ if(e.keycode == 38){ clicksound.pause(); playing=false; } }
code not tested!
javascript onkeyup
Comments
Post a Comment