2015.11.01 11:43
programming puzzle: why does it play only once?

Here’s a programming puzzle. Watch this program: https://jsfiddle.net/jj95xpxn/1/. Tell me why it plays the recording only once, not three times. And fix it. Answers in rot13, please.
function playSound(fileName) {
  var deferred = jQuery.Deferred();
  var sound = new Audio(fileName);
  $(sound).bind('ended', function () {
    deferred.resolve();
  });
  sound.play();
  return deferred.promise();
}

function playSounds(sounds) {
  var playFinishedPromise = playSound(sounds[0]);
  var i = 1;
  while (i < sounds.length) {
    playFinishedPromise = playFinishedPromise.then(function () {
      return playSound(sounds[i]);
    });
    ++i;
  }
}

$(function () {
  var ogg = 'https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg';
  playSounds([ogg, ogg, ogg]);
});


comments:
2015.11.10 08:18 ŁZ

uggcf://wfsvqqyr.arg/dcjox7aY/1/ ;)


2016.01.17 10:18 fdreger

Nie rotuję, bo to chyba nie podpowiedź, że wszystko byłoby łatwiejsze, gdyby trzymać się funkcji - i w playSounds zrobić:

sounds.reduce(function(a, b){
  return a.then(function(){
    return playSound(s)
  })
}, Promise.resolve())




2016.01.17 10:20 Piotrek

@fdreger: a mówiłeś, że nie masz w domu Internetu.



nickname:

enter digit "four": (this is a spam protection)

offensive comments or those I don't like will be deleted


back to homepage

RSS