0

I am trying to fix a problem with my recursive function JS using a call back. I just want to update the HTML of 3 div using according to an index. Please find the code below

<div id="try0">50</div>
<div id="try1">50</div>
<div id="try2">50</div>

function getNumberOfAnswers(questionID, callback)
{
  var value = i*10;
   callback( value.toString());

}

var i=0;
getNumberOfAnswers(i, function callFunc(ratio){
    var itemToChg = 'try'+i;
document.getElementById(itemToChg).innerHTML = ratio;
    if(i<3){
        i++;
        getNumberOfAnswers(i,callFunc(ratio));

    }

    });

I didn't put any tags on the code above to simplify but I made a JSfiddle with it. http://jsfiddle.net/cyrilGa/zmtQ8/ . On the third line from the end, I tried to write getNumberOfAnswers(i,ratio); but it didn't work. Can somebody help me with this Cheers

6
  • I don't know if it's possible to fix this imbricated call/declaration structure but I think you just choose the most complex way to do a simple thing. Can you explain what you exactly try to do ? Commented Jun 26, 2012 at 11:28
  • Saying "it didn't work" is useless. What did you expect? What actually happened? What errors did you get? Commented Jun 26, 2012 at 11:35
  • My actual problem is actually is bit more complex than the one above so I tried to simplify it. I am querying a parse.com database using Javascript asynchronously 10 times. So I have to wait for the end of one query (hence the callback) before querying it again. (hence the recursive part). With each result from the database, I am updating the value inside a div. Hope this is clear Commented Jun 26, 2012 at 11:41
  • You are right, I should have explained a bit more, I expected to have 0 in the div Try0, 10 in the Try1, and 20 in the Try3. I get 0 on all divs. So it is not incrementing the index Commented Jun 26, 2012 at 11:43
  • The callback calls back with the given ratio instead of referencing the function: getNumberOfAnswers(i,callFunc); Commented Jun 26, 2012 at 12:04

3 Answers 3

2

The line:

var value = i*10; 

should be

var value = questionID * 10;

And I think

getNumberOfAnswers(i,callFunc(i));

Should be:

getNumberOfAnswers(i,callFunc);
Sign up to request clarification or add additional context in comments.

Comments

1

Do not use recursion for this, it is silly.

for ( var i = 0; i < 3; i++ ) {
    document.getElementById('try' + i).innerHTML = i * 10;
}

Is this what you want?

1 Comment

My actual problem is actually is bit more complex than the one above so I tried to simplify it. I am querying a parse.com database using Javascript asynchronously 10 times. So I have to wait for the end of one query (hence the callback) before querying it again. (hence the recursive part). With each result from the database, I am updating the value inside a div. Hope this is clear
0

You need to replace the recursive callFunc(ratio); at the bottom to callFunc(i); because the argument ratio is still equal to 0 while you increment i. Everything else is fine.

1 Comment

When I use callFunc(i), the function actually returns i. What I expect is to get i*10;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.