I have created this script in JS that when a button is pressed, an audio file is played repeatedly. For context, the audio file is some 'beep' and is used as sort of an alarm. The speed at which the beep must be played is depending on a setting which can be found on a page called 'sound.php'.
Now this script is working partly. Whenever the sound-producing page is loaded up, and the 'Play' button is pressed, the beeping starts and it is audible at the correct frequency.
However, when the time interval on sound.php is changed in real time, the frequency of the beeping does not change with it.
I believe that the script as it is now, will retrieve the interval from sound.php every 1000ms but something is not right and I cannot get it to work.
$(document).ready(function () {
ajax_call = function() {
$.ajax({
type: "GET",
url: "sound.php",
dataType: "html",
success: function (response) {
soundValue = response;
}
});
};
var interval = 1000; //refresh every second
setInterval(ajax_call, interval);
});
function play() {
const intervalTime = soundValue; // Interval time in milliseconds
const interval = setInterval(function() {
var audio = new Audio('beep.mp3');
audio.play();
}, intervalTime);
setTimeout(function() {
clearInterval(interval);
}, 900000);
}
What am I missing here? Small thing or a major flaw that I'm not overseeing? Any help is much appreciated.