1
I am trying to update some questions on a page, going through an _for with the number of steps equal to the total of questions, to inject in each passage a question via $.post(), and wait for the return to only then continue the loop. I don’t know if this is the best way to do it but for this situation I have, two problems have arisen:
- Find external variable inside a Function
- Prevent loop flow until loop operation $.when complete
The code I’m using is below:
function recalcularPesos(){
//courseid=
var id = ['1','2','3','4','5'];
var passo = 0;
for (p = 0; p < id.length; p++)
{
passo = passo+1;
$.post("https://test.site.net/mod/quiz/edit_rest.php?class=resource&courseid="+id[p]).then(function()
{
console.log( passo+"a questão foi modificada" );
if(passo = id.length){
console.log('passo: '+passo);
//Atualizar página após todas as atualizações
setTimeout(location.reload(), num*2000);
}
}, function() {
alert( passo+"a questão NÃO modificada" );
}
);
}
}
recalcularPesos();
The way I’m doing it needs to refresh the page to show the updated values of the questions, but the page is being updated even before completing all the posts (if there are too many questions, worse). I also noticed that the variable footstep returns in the terminal at all steps of the iteration, the maximum value (5,in that case).
Got a little confused. What are you trying to do?
– Woss
Where does that come from
courseid
?– Sam
My real goal is to send the new notes of the questions to modify them, but as in the original code there were many searches/parameters (like the id of the question, variable with number of questions, page session), I decided to dry the code above. In short. I need every step of for inject for $.post() the question data with your new note, but only go to the next step when you complete the previous one, and only at the end of all, refresh the page. The problem there is that I can’t find the variables footstep and id within the Function more internal. P.S. I am layman.
– GOliveira
Corrected the variable name. It’s because I reduced the code to make it clearer where the problem would be, and I ended up confusing their names.
– GOliveira