javascript - Unexpected Callback Behavior in jQuery -


i expect following code run follows: 1) iterate through loop until complete. 2) execute callback function

instead, executes callback after each iteration. scope issue? misunderstanding control flow of callbacks?

function flippages(direction,n,duration,callback){     for(i=0;i<n;i++){         settimeout(function() { $('#flipbook').turn(direction);}, ((i+1) * duration));     }     callback; }  flippages("next",4,1000,flippages("previous",4,2000)); 

in addition these relevant comments, guess wanted :

function flippages(direction, n, duration){     for(var = 0, l = n; < n; i++){         settimeout(function() {              $('#flipbook').turn(direction);             if (!(--n)) {                 flippages({                     // direction switch                     next: "previous",                     previous: "next"                 }[direction], n, duration);             }         }, ((i + 1) * duration));     } }  flippages("next", 4, 1000); 

updated : no need use callback, use flippages directly.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -