I need to create a purposeful loop to check if a variable has reached the desired value, the idea is to create a function that inside it will have a if
, which verifies if cont == 4
in case 4 is the number of iterations previous to function, if it continues the process if it is not yet 4
it gives a setInterval
passing by parameter to função
in 500 milissegundos
.
Below the code:
function verificaCont(){
if(data.cont == 4){
console.log(data);
}else{
setInterval(verificaCont(), 500);
}
}
The error that occurs when executing this function is:
Uncaught SyntaxError: In strict mode code, functions can only be declared at top level or immediately within another function.
How can I solve this problem? or is there something for me to have to generate a loop?
REASON:
I need this loop because I'm doing an activity that wants to save in a multiple data object, between these data it may be necessary to go to the database, however the bank used is asynchronous, so I need a loop to give it time to fill the whole object.