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
Post a Comment