As mentioned in the title, I am trying to create a jQuery / JavaScript metric, as well as an HTML tag <audio />to play the sound.
It works "well", but it seems to me that the method setIntervaldoes not work accurately enough. I searched for some topics here, but I'm new to jQuery and JavaScript, and I have not found a working solution. The same for "open new tab and setInterval stops or lags" - the problem. I tried to prevent this with help stop(true,true), but it did not work as I expected.
I want the metronome to start “in the background” without changing the tempo when opening a new tab and doing something there. I also want to say exactly the metronome;)
Here is my test environment: http://nie-wieder.net/metronom/test.html
JS-Code and HTML markup are currently in the source of test.html, so you can look there.
In addition, here is the interested (as it seems to me) js code that I use:
$(document).ready(function() {
var intervalReference = 0;
var currentCount = 1;
var countIncrement = .5;
var smin = 10;
var smax =240;
var svalue = 120;
$(".sndchck").attr("disabled", true);
$.ajax({
url: "snd/tick.ogg",
success: function() {
$(".sndchck").removeAttr("disabled");
}
});
var met = $("#bpm").slider({
value: 120,
min: smin,
max: smax,
step: 1,
change: function( event, ui ) {
var delay = (1000*60/ui.value)/2
clearInterval(intervalReference);
intervalReference = setInterval(function(){
var $cur_sd = $('#sub_div_'+currentCount);
$cur_sd
.stop(true,true)
.animate({opacity: 1},15,
function() {
if($('#sound_check:checked').val()){
$('#tick')
.stop(true,true)
.trigger("play");
}
$(this).
stop(true,true).
animate({opacity:0});
}
);
currentCount += countIncrement;
if(currentCount > 4.5) currentCount = 1
}, delay);
createMusicTag(ui);
}
});
});
Any help would be great, I have no ideas yet.