JSFiddle - React, Tailwind, and code Playground
by Avi Algaly
HTML
<button type="button">Beep!</button>
JavaScript
var beep = (function () {
var ctxClass = window.audioContext ||window.AudioContext || window.AudioContext || window.webkitAudioContext
var ctx = new ctxClass();
return function (duration, type, finishedCallback) {
duration = +duration;
// Only 0-4 are valid types.
type = (type % 5) || 0;
if (typeof finishedCallback != "function") {
finishedCallback = function () {};
}
var osc = ctx.createOscillator();
osc.type = type;
//osc.type = "sine";
osc.connect(ctx.destination);
if (osc.noteOn) osc.noteOn(3);
if (osc.start) osc.start();
setTimeout(function () {
if (osc.noteOff) osc.noteOff(0);
if (osc.stop) osc.stop();
finishedCallback();
}, duration);
};
})();
document.getElementsByTagName("button")[0].addEventListener("click", function () {
var button = this;
button.disabled = true;
beep(50, 12, function () {
button.disabled = false;
});
});