1

I have 2 functions that should run one after another. I am implementing a callback in the first one, but it looks like something (I think the getJSON or the forEach) inside this function is messing around.... here is the code :

function one (param1, param2, callback)
{
     ...
     $.getJSON(...function(data){});
     my_array.forEach(function(item){...});
     if(callback) callback();
}

function two ()
{
    ...
}

Then I call the function this way :

one(value1, value2, two)

When I do that, I notice that my function two is using some values that have not yet been processed by function one.... I could put function two in the callback of the getJSON, but I must have the forEach performed two....

What am I doing wrong ?

EDIT :

var next = function (item, id) {
item = item.substr(2);
item = item.substr(0, item.length - 2);

        $('#popup_generate').html(item + ' <input type="text" id="question_answer" name="question_answer"><br><br><button class="close">Valider</button>');
        $('#popup_generate').fadeIn().css({'width': 400});

        $('body').append('<div id="fade"></div>');
        $('#fade').css({'filter': 'alpha(opacity=70)'}).fadeIn();
        $('.close, #fade').click(function () {
            answer = $('#question_answer').val();
            inputText = inputText.replace('[[' + item + ']]', answer);
            tinyMCE.activeEditor.setContent(inputText);

            if(++id < resultArray.length) {next(resultArray[id], id);} else {$('#fade , .popup_block').fadeOut(function () {$('#fade, .close').remove();});}
        });

    };
    next(resultArray[0], 0);
Vincent Teyssier
  • 2,146
  • 5
  • 25
  • 62
  • It would be useful to have the full code, to see what operations you're doing on your parameters etc. – jonny Aug 21 '15 at 09:18
  • you should call your callback after check value is resolved or not – Shailendra Sharma Aug 21 '15 at 09:20
  • Ok, from the dup question I understand that I should put my forEach inside the callback of the getJSON. But how will I do for the forEach... it's not really a forEach, it is a function that repeats itself a number of times, I put the code in the edit – Vincent Teyssier Aug 21 '15 at 09:36

0 Answers0