Note that there are some explanatory texts on larger screens.

plurals
  1. POCallbacks with parameters
    primarykey
    data
    text
    <p>I have jQuery function:</p> <pre><code>$('#fetch').click( function() { ... GetPage( curPage, items, DrawItemSet( items ) ) }); </code></pre> <p>As you can see, I want the callback function DrawItemSet be executed from inside <strong>GetPage</strong> function. Sometimes, I need to call not <strong>DrawItemSet</strong> but some other functions.. I just want to any function I need to be called from <strong>GetPage</strong> function. It is necessary to do some afterwork after (as you may guesses) I get the page. In this example I want to display some data. In other cases I want not to display some text, but, for example, swap information got from <strong>GetPage</strong> function. In that case, my idea was to write something like:</p> <pre><code>... GetPage( curPage, items, SwapItemSet( oldItems ) ) </code></pre> <p>As fo now, everything seems to be ok.</p> <p>Here's my <strong>GetPage</strong> listing:</p> <pre><code>function GetPage( pageNum, collection, callback ) { console.log( 'Starting to get page number ' + pageNum ) $.ajax({ url: 'http://www.xxxxxxxxxxx.php?page=' + pageNum, type: 'get', dataType: '', success: function(data) { ... console.log( 'Curpage=' + curPage + ' MaxPage=' + maxPages ) if( curPage &lt; maxPages ) { curPage = curPage + 1 GetPage( curPage, collection, callback ) } else { console.log( 'passed' ) callback() } } }); } </code></pre> <p>As you can see this function calls itself while there are some pages left to fetch, and if there's no page left, the callback function should be called to do some stuff. The callback example you can see below (just for testing):</p> <pre><code>function DrawItemSet() { console.log( 'DrawItemSet called.' ) ... } </code></pre> <p>My problem is that in theory everything looks ok, and I should get in the console <strong>DrawItemSet</strong> as the last message meaning that the callback function was called after the fetching is being finished. Well.. instead of it I get the following:</p> <pre><code>DrawItemSet called. Starting to get page number 1 Curpage=1 MaxPage=2 Starting to get page number 2 Curpage=2 MaxPage=2 passed </code></pre> <p>And this means that somehow the callback is the first function being executed.</p> <p>How so?!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload