1
I am developing a system using angular and have the following function:
function drawLatLong(i, arrayIdColetor) {
.....
(não exibi o código por ser grande e achar desnecessário que vcs o analisem)
.....
});
This function is invoked in a for:
for(var j = 0; j < $kinvey.arrayIdColetor.length; j++){
drawLatLong(j, $kinvey.arrayIdColetor);
}
The function drawLatLong that is invoked N times according to FOR, it takes a considerable time to be executed as it does relatively heavy searches in the database.
The problem I’m having is that if the user invokes another function without that function drawLatLong has finished, the system does not behave as it should.
Would there be any way to keep this situation under control? That is, only allow the user to perform another function after the drawLatLong has been finalized? Or it would be possible to interrupt the execution of the function drawLatLong in between, if another function is invoked by the user?
I hope I made myself clear. Thanks in advance.
You were of course yes. I never thought of calling a call function for the bank in a loop. We can start the solution by taking this call from inside the loop. Imagine if you get stuck in this loop and it gets infinite.... crash in your database.
– durtto
a simple "if" condition would not solve your problem?
– Ivan Ferrer
@durtto I need to make this call inside the loop because I search for latitude and longitude according to the idColetor (1 idColetor identifies 1 vehicle), so if I have 3 idColetors (or 3 vehicles) I need to pick up the coordinates of each of the vehicles and print on the screen, I’m not sure I made myself clear. Abs.
– S_A
@Ivanferrer using an if how could I interrupt the execution of one function in the middle to execute another that was invoked by the user? Obg
– S_A
The idea is not to interrupt the script, but to get out of it...
– Ivan Ferrer
To interrupt you can use
try { ...} catch(err) { ... } finally { ... }
.– Ivan Ferrer
Another thing you can do is give one
break;
in your noosefor
, if a condition or other function is invoked.– Ivan Ferrer
@S_A, thought to run the
drawLatLong
on a Webworker? by doing this, you will not occupy your main thread.– Tobias Mesquita
@Tobymosque follows what I want to do: when opening the screen I search in the bank the latitude and longitude of several vehicles (for this I use the
for(var j = 0; j < $kinvey.arrayIdColetor.length; j++){ ... }
. The functiondrawLatLong
is responsible for displaying these coordinates on the screen, and each vehicle will have its outline. The problem is that while this is done, it may be that the user selects to see the tracing of only 1 vehicle, thus the function of displaying only one tracing runs in parallel with the functiondrawLatLong
that is still running. I think Webworker n would help. What do you think? Obg– S_A
look, this will depend on the implementation, when loading the page you can use a
WebWorker
by vehicle and load thelongitude
and thelatitude
in parallel and only release the page when allWebWorker
send the return message... this way the method to display only one stroke will not need to redo the query, just apply a filter on the page... but as said, everything depends on its implementation.– Tobias Mesquita
It will probably work @Tobymosque, but first I will try some alternative that requires me less changes to the code. Obg
– S_A