Question :
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.
Answer :
As I said, it’s to give you a complete answer, it would be interesting if you put your code with IndexedDB, so I’ll just post a generic answer.
For example, let’s imagine two Tables, Users and People, where Users have a 1: N relationship with People and we just want the User Login and Person Name.
var transaction = db.transaction(["usuarios", "pessoas"]);
var usuarioStore = transaction.objectStore("usuarios");
var pessoaStore = transaction.objectStore("pessoas");
var UsuarioModel = function(usuarioID, callback) {
var self = this;
self.UsuarioID = usuarioID;
var usuarioRequest = usuarioStore.get(self.UsuarioID);
usuarioRequest.onsucess = function (event) {
self.Logon = usuarioRequest.result.Logon;
var pessoaRequest = pessoaStore.get(usuarioRequest.result.PessoaID);
pessoaRequest.onsucess = function (event) {
self.Nome = pessoaRequest.result.Nome;
callback();
}
}
}
var usuarioModel = new UsuarioModel(35, function () {
console.log(usuarioModel);
});
In the above example, the function of callback
will be executed Name is retrieved.
Now let’s imagine a second scenario, the entity Users and People are independent, but need to wait for the return of the two requests to execute some code.
var transaction = db.transaction(["usuarios", "pessoas"]);
var usuarioStore = transaction.objectStore("usuarios");
var pessoaStore = transaction.objectStore("pessoas");
var UsuarioModel = function(usuarioID, pessoaID, callback) {
var self = this;
var sucessos = 0;
var execCallBack = function () {
sucessos++;
if (sucessos == 2) {
callback();
}
}
self.UsuarioID = usuarioID;
self.PessoaID = pessoaID;
var usuarioRequest = usuarioStore.get(self.UsuarioID);
usuarioRequest.onsucess = function (event) {
self.Logon = usuarioRequest.result.Logon;
execCallBack();
}
var pessoaRequest = pessoaStore.get(self.PessoaID);
pessoaRequest.onsucess = function (event) {
self.Nome = pessoaRequest.result.Nome;
execCallBack();
}
}
var usuarioModel = new UsuarioModel(35, 58, function () {
console.log(usuarioModel);
});
setInteval
is a function that performs a particular / strong> in a time interval.
If you want to do this, you should use setTimeout
that only runs once after the break.
Code
function verificaCont(){
if(data.cont == 4){
console.log(data);
}else{
setTimeout(function(){
verificaCont();
}, 500);
}
}
Alternative
function verificaCont(){
this.checkCount = setInterval(function(){
if(data.cont == 4){
clearInterval(this.checkCount);
}
}, 500);
}