html5 play audio loop 4x then stop

后端 未结 5 2245
既然无缘
既然无缘 2020-12-12 03:14

Hi I need to loop audio file a certain number of times ie 4, I have the sound file looping correctly, code below. ? how to stop loop after 4x, many thanks P

         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 03:52

    I managed to do it like this:

     $('#play_audio').click(function () {                     
                var times = 4;
                var loop = setInterval(repeat, 500);
    
            function repeat() {
                times--;
                if (times === 0) {
                    clearInterval(loop);
                }
    
                var audio = document.createElement("audio");
                audio.src = "files/windows%20default.mp3";
    
                audio.play();
            }
    
            repeat();           
        });      
    }); 
    

提交回复
热议问题