Reputation: 1223
How can i play a sound at an interval (for example once every second) and gradually decrease the time between the intervals?
At the moment, i use two intervals with setInterval, the first one plays the sound each second, the second setInterval speeds up the first interval every 20 seconds. This works, but it leaves a nasty "pause" between the intervals.
Is there a better way?
Example code (just to clarify, not necessary to read):
var audio = new Audio('track.wav');
var baseSpeed = 1000;
var myInt;
var changeInt;
//Starts a Run
function beatInterval() {
audio.play();
};
//Speeds up the other interval
function speedUpInterval() {
baseSpeed = baseSpeed - 20
clearInterval(myInt);
myInt = setInterval(beatInterval, baseSpeed);
console.log(baseSpeed);
myInt = setInterval(beatInterval, baseSpeed);
changeInt = setInterval(speedUpInterval, 20000);
Upvotes: 1
Views: 723
Reputation: 15278
You can delay adjustment of speed, until interval function is actually executed:
var speedChanged = false;
function beatInterval() {
if(speedChanged) {
speedChanged = false;
clearInterval(myInt);
myInt = setInterval(beatInterval, baseSpeed);
}
audio.play();
};
function speedUpInterval() {
baseSpeed = baseSpeed - 20
speedChanged = true;
console.log(baseSpeed);
}
Upvotes: 3