Skip to content

Commit 19c00fc

Browse files
committed
Merge pull request #213 from aileo/master
Adding callback to artoo.beep
2 parents 6840826 + ea532d5 commit 19c00fc

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/artoo.beep.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,22 @@
2626
}
2727

2828
// Playing the base64 sound
29-
artoo.beep = function(sound) {
30-
var chosenSound;
29+
artoo.beep = function(a1, a2) {
30+
var sound,
31+
callback,
32+
chosenSound;
33+
34+
if (typeof a1 === 'function') {
35+
callback = a1;
36+
}
37+
else {
38+
sound = a1;
39+
if (typeof a2 === 'function')
40+
callback = a2;
41+
else if (typeof a2 !== 'undefined')
42+
throw Error('artoo.beep: second argument have to be a function.');
43+
}
44+
3145
if (artoo.helpers.isArray(sound))
3246
chosenSound = randomInArray(sound);
3347
else
@@ -39,7 +53,12 @@
3953
if (!~sounds.indexOf(chosenSound))
4054
throw Error('artoo.beep: wrong sound specified.');
4155

42-
new Audio(artoo.settings.beep.endpoint + chosenSound + '.ogg').play();
56+
var player = new Audio(artoo.settings.beep.endpoint + chosenSound + '.ogg');
57+
if(callback)
58+
player.addEventListener('ended', function() {
59+
callback();
60+
});
61+
player.play();
4362
};
4463

4564
// Exposing available beeps

0 commit comments

Comments
 (0)