3
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 turns out to be cont == 4
in case 4 is the number of iterations before the function, if it is it continues the process if it is not yet 4
he of the one 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 happens when performing 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 I can have to generate a loop ?
MOTIVE:
I need this loop because I am doing an activity that intends to save in a multiple object data, between these data may be necessary to go to the database, but the database used is asynchronous, for this reason I need a loop to allow time to fill the whole object.
Updates the question with the for code you have.
– Vinícius
@Renan, JS was made to work asynchronously, could you tell what is the server-side language used to access the database? because I believe this loop is unnecessary, and what it needs is a callback function.
– Tobias Mesquita
@Tobymosque I use Indexeddb, and when I go to the search bank it continues to pass to the bottom line, and when I enter it does not enter the data. Even if the data is in the object.
– Renan Rodrigues
@Renanrodrigues, when you make a request to an objectStore, you have two events. " sucess" and "error", so you can perform a callback function in the request "sucess". If you are making multiple requests, then create an internal counter, and when it reaches the number of requests made, you call the callback function.
– Tobias Mesquita
so it’s interesting that you add the question to your code snippet that accesses Indexeddb, so I can suggest a better way to work with it.
– Tobias Mesquita
@Tobymosque how would it look ? would you show me an answer ?
– Renan Rodrigues