Calling a function infinitely

Calling a function infinitely
0.0 0

#1

Hi people ! I’m at the Pomodoro challenge, I did great so far but I need to repeat the length of session (25 min) then pause (5min) again and again, the problem is that I can only repeat the session, then the pause, and it just run the pause infinitely, I will explain more but here is my js code :

`
var session = 7;
var pause = 5;
var secEnString = "00"
var sec = 0;
var time;

document.getElementById(“display”).innerHTML = session;
document.getElementById(“display2”).innerHTML = secEnString;
document.getElementById(“displaySession”).innerHTML = session;
document.getElementById(“displayPause”).innerHTML = pause;

let addOne = () => {
session++;
console.log(session);
document.getElementById(“display”).innerHTML = session;
document.getElementById(“displaySession”).innerHTML = session;
}
let minusOne = () => {
session–;
console.log(session);
document.getElementById(“display”).innerHTML = session;
document.getElementById(“displaySession”).innerHTML = session;
}
let addOne1 = () => {
pause++;
console.log(session);
document.getElementById(“displayPause”).innerHTML = pause;

}
let minusOne1 = () => {
pause–;
console.log(session);
document.getElementById(“displayPause”).innerHTML = pause;

}

function decompte () {
if(time) {
clearInterval(time);
time = null;
sec = 0;
session = document.getElementById(“displaySession”).innerHTML;
document.getElementById(‘display’).innerHTML = session;
document.getElementById(‘display2’).innerHTML = sec;
document.getElementById(‘SR’).innerHTML = “START”;
}
else {
var retiens = session;
console.log(retiens);
time = setInterval(function() {
function repeat() {

   if(sec === 0) {
   	sec = 60;
   	retiens--;
   }
	sec--;
	if (retiens === 0 && sec === 0) {
		retiens = pause;
	}

console.log(“hi!”);
}
repeat();

	document.getElementById('SR').innerHTML = "RESET";
	document.getElementById('display').innerHTML = retiens;
	document.getElementById('display2').innerHTML = sec;
	/*
	if(sec === 0) {
		clearInterval(time); //c'est là que tu mettras session = pause;
	}*/
}, 10)

}

}
`

and here is my HTML :

`

PAUSE
SESSION

-1

5

+1

<div onclick="decompte()" id ="result" class="el">
	<p id="SR">START</p>
	<div id="flexResults">
    <p  class="flexres" id="display">  </p><p class="flexres"  id="deuxpoints">:</p><p class="flexres"  id="display2">  </p>
    </div> 
</div>

<div id="sessionMinusOne" onclick="minusOne()" class="el"><p>-1</p></div>
<div  class="el"><p id="displaySession"></p></div>
<div id="sessionAddOne"  onclick="addOne()" class="el"><p>+1</p></div>
`

I will create a Codepen for your understanding :
https://codepen.io/Ziratsu/pen/jzVyrV

I think I need a callback function in the “decompte” function :

`else {
var retiens = session;

time = setInterval(function() {
	function repeat() {
		
   if(sec === 0) {
   	sec = 60;
   	retiens--;
   }
	sec--;
	if (retiens === 0 && sec === 0) {
		retiens = pause;
	}

console.log(“hi!”);
}
repeat();`

I’ve tried to do infinite loop with some “while”, but it just make chrome crashed indeed ! :slight_smile:
I hope I’m clear, tell me if i’m not.
If someone can give me a hand, i’ve barely made it I just need to enchain length of session and length of pause, infinitely.


#2

@Ziratsu,
after briefly reading your code, I want to ask why by just attaching an event listener for each button is not enough? There is no need for an infinite loop if for each time the pomodoro clock starts a new session, the values of the intervals are overridden.

Meaning, set the time intervals for the break and work sessions every time the start button is pressed, since you are supposed to handle one session each time (to the best of my knowledge).


#3

You mean I need to let the user handle sessions himself? Like when the pause is over I let him click the button to start another session ?
I can add some sound when session is over to help the user too