Access variable in and out of a javascript function?

Asked

Viewed 1,289 times

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:

  1. Find external variable inside a Function
  2. 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?

  • Where does that come from courseid?

  • 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.

  • 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.

1 answer

2


Updating: Use for is not recommended as being asynchronous, I mean, you’ll be looping without waiting for the return of $.when.

The location.reload() in the setTimeout must be in quotes.

You can reset the function recalcularPesos() whenever the URL invoked on $.post send a positive feedback loop to the limit of items in the array id:

var id = ['1','2','3','4','5'];
passo = 0;
function recalcularPesos(){
    if(passo < id.length){
        $.post("https://test.site.net/mod/quiz/edit_rest.php?class=resource&courseid="+id[passo]).then(
        function(){
            console.log( id[passo]+" a questão foi modificada" );
            passo++;
            recalcularPesos();
        },
        function() {
            console.log( id[passo]+" a questão NÃO modificada" );
        });
    }else{
        // aqui termina o loop
        setTimeout("location.reload()",2000);
    }
}

recalcularPesos();

Note that I put the Array out of function(!) so it doesn’t keep repeating.

  • Boy! That’s what I was looking for! And thanks for the many tips my dear @Dvdsamm!

  • 1

    @Gabrieloliveira Pleasure to have been helpful! Good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.