Skip to content

Instantly share code, notes, and snippets.

@fgutz
Last active December 30, 2015 01:59
Show Gist options
  • Save fgutz/7759301 to your computer and use it in GitHub Desktop.
Save fgutz/7759301 to your computer and use it in GitHub Desktop.
var Warning = (function(){
// web Audio API object
var sound = {
init : function() {
this.context = new webkitAudioContext();
this.oscillator = this.context.createOscillator();
this.oscillator.connect(this.context.destination);
this.oscillator.type = 3;
this.oscillator.frequency.value = 266;
},
play : function(num){
var n = num || 0;
var that = this;
this.modulate = setInterval(function(){
var f = (that.oscillator.frequency.value === 266) ? 238 : 266;
that.oscillator.frequency.value = f;
},200);
this.oscillator.start(n);
},
stop : function(num){
var n = num || 0;
this.oscillator.stop(n);
clearInterval(this.modulate);
}
};
// functions for checking if server limits are too damn high
var _limit = 10,
boxes = document.getElementsByClassName('box'),
klaxon = 0,
intvl;
var warning = function(item,elm){
if (item > _limit) {
console.log(elm.children[1].textContent + ' is over ' +_limit);
if (klaxon === 0){
sound.play();
klaxon = 1; // once warning sound has started we don't need to reload it for concurrent warnings
}
}
};
var updateLimit = function(l){
_limit = l || 10;
};
var check = function(){
for (var i=0,len=boxes.length; i < len; i++) {
var target = boxes[i].children[3].children[0];
var val = parseFloat(target.textContent);
warning(val,boxes[i]);
}
};
var begin = function(time){
sound.init();
var t = time || 15;
var finalT = t * 1000;
intvl = setInterval(check, finalT);
};
var kill = function(){
clearInterval(intvl);
sound.stop();
klaxon = 0;
};
return {
begin : begin,
kill : kill,
stop : sound.stop,
updateLimit : updateLimit
};
});
window.warning = new Warning();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment