Back to Parent

function playMusic() {
  var PLAY_TIME = 60 * 5; // 5 minutes
  var PLAY_CHANCE = 0.5;  // probability of playing
  var WAIT_TIME = 5;      // seconds before executing loop
  var timePassed = 0;
  var songs = [document.getElementById('song1'),
               document.getElementById('song2'),
               document.getElementById('song3'),
               document.getElementById('song4')];

  for (var i = 0; i < 4; i++) {
    songs[i].play();
  }
  var playRandomly = function() {
    timePassed += WAIT_TIME;
    for (var i = 0; i < 4; i++) {
      if (Math.random() < PLAY_CHANCE) {
        songs[i].muted = true;  // mute song
      } else {
        songs[i].muted = false; // unmute song
      }
    }
    if (timePassed < PLAY_TIME) {
      setTimeout(playRandomly, 1000 * WAIT_TIME);
    }
  };
  playRandomly();
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0