Repetitive Function

Joined
Mar 23, 2023
Messages
2
Reaction score
0
var seconds = 0;
timedLoop(1000, function() {
seconds = seconds + 1;
console.log(seconds + " second(s)");
});
if (seconds == randomNumber(1, 20)) {
playSound("sound://category_accent/puzzle_game_accent_a_01.mp3", false);
}

The purpose of this function is to play sound at a random time while the timer initiates. The problem is that the playSound is repeated with the other variables, which is not content for my program. I have tried many ways to fix the issue, such as putting the playSound outside the function, but then the program never reaches or ignores the playSound since once it gets into the time loop function, it continually repeats itself. I have also tried putting a return function right above the playSound, so it doesn't repeat it, but it never reaches the playSound. If there are any suggestions, feel free to propose.
 
Joined
Mar 23, 2023
Messages
2
Reaction score
0
var seconds = 0;
timedLoop(1000, function() {
seconds = seconds + 1;
console.log(seconds + " second(s)");
if (seconds == randomNumber(1, 20)) {
playSound("sound://category_accent/puzzle_game_accent_a_01.mp3", false);
}
});
 
Joined
Sep 4, 2022
Messages
127
Reaction score
16
one thing : the 'seconds' are always increasing ( +1 ) at firing.
reset your seconds ( seconds = 0 ; ) after the sound is play.

it makes seconds bigger than the 20 high limit you choose. so the script will run for nothing after 20 seconds.
( I could be wrong if all your code is in a outer function )

your second version looks right, isnt it ?
 
Joined
Jul 4, 2023
Messages
350
Reaction score
40
According to FResher answer ... workaround can be

e.g.

JavaScript:
var seconds = 0;
timedLoop(1000, function() {
  seconds += 1;
  console.log(seconds + " second(s)");
  if (seconds == randomNumber(1, 20 + seconds)) {
    playSound("sound://category_accent/puzzle_game_accent_a_01.mp3", false);
  }
});

or

JavaScript:
var seconds = 0;
timedLoop(1000, function() {
  if (++seconds == randomNumber(1, 20 + seconds))
    playSound("sound://category_accent/puzzle_game_accent_a_01.mp3", false);
  console.log(`${seconds} second(s)`);
});
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top