Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <body>
- <h2>My First JavaScript</h2>
- <button type="button"
- onclick="document.getElementById('demo').innerHTML = Date()">
- Click me to display Date and Time.</button>
- <p id="demo"></p>
- <input type="text" id="txt">
- <button onclick="arraypush()">get</button>
- <p>Play: <i id="keberapa"></i></p>
- <script>
- let arrayaudio =[];
- var isplay = false;
- function play(audio, callback) {
- audio.play();
- if (callback) {
- //When the audio object completes it's playback, call the callback
- //provided
- arrayaudio.shift();
- audio.addEventListener('ended', callback);
- }
- }
- //Changed the name to better reflect the functionality
- function play_sound_queue() {
- var index = 0;
- function recursive_play() {
- //If the index is the last of the table, play the sound
- //without running a callback after
- // if (index + 1 === arrayaudio.length) {
- // play(arrayaudio[index], null);
- // } else {
- //Else, play the sound, and when the playing is complete
- //increment index by one and play the sound in the
- //indexth position of the array
- play(arrayaudio[index], function() {
- index++;
- if (index + 1 === arrayaudio.length) {
- // play(arrayaudio[index], null);
- recursive_play();
- }
- // isplay = false;
- });
- // }
- }
- //Call the recursive_play for the first time
- recursive_play();
- }
- /*
- function recursive_play() {
- var index = 0;
- //Else, play the sound, and when the playing is complete
- //increment index by one and play the sound in the
- //indexth position of the array
- play(arrayaudio[index], function() {
- index++;
- if (index + 1 === arrayaudio.length) {
- play(arrayaudio[index], null);
- isplay = false;
- }else {
- recursive_play();
- }
- });
- }*/
- function arraypush(){
- var txtisi = document.getElementById('txt').value;
- arrayaudio.push(new Audio(txtisi));
- console.log("1",txtisi);
- if(!isplay){
- isplay = true;
- console.log("2",txtisi);
- play_sound_queue();
- }
- alert(arrayaudio);
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement