Skip to content Skip to sidebar Skip to footer

Javascript: Wait For Function In Loop To Finish Executing Before Next Iteration

$(function() { $('#submit').click(function() { for (var i=1; i<=14; i++) { setID(i); checkField(i); } if ($('#pass_fail').val() != 'fail') { //do something

Solution 1:

I'm assuming from your description of the issue that checkField() is making some sort of asynchronous Ajax call. If you truly want to serialize the calls to checkField() so the 2nd call is not made until the first has completed, then you need to change the structure and flow of your code in order to do that. The two common ways of doing so are either using a callback that indicates completion of a call to checkField() that triggers the next call to checkField() or the use of promises to accomplish something similar.

Here's an example of the callback mechanism:

$("#submit").click(function() {
    var i = 0;
    functionnext() {
        i++;
        if (i <= 14) {
            setID(i);
            checkField(i, next);
        } else {
            // all checkField operations are done nowif ($('#pass_fail').val() != "fail") { 
                //do something
            }
        }
    }
    next();
});

Then, checkField() would have to be modified to call the callback passed to it when it completes its asynchronous operation.


If you're using jQuery to make the ajax operations inside of checkField, it would be fairly easy to use jQuery promises to solve this also.

Solution 2:

Sorry, but in earlier response I didn't see any loop to wait for any other function or any inner loop execution to let it finish before next iteration.

With JQuery 3.3.1

Scenario: for loop with var i will wait for inner other loop to complete before next iteration value of i

$(document).ready(function(){

    $("p").click(function() {
    
    	var varIDeferred = $.Deferred();
        var varJDeferred = $.Deferred();
        
        /*
        var loopJInside = function(j) {  
            console.log("j is : " + j);
            $("span").html($("span").html() + "<br>j is : " + j);
            varJDeferred.resolve();
            j++;
            console.log("j value after incr is : " + j);
            return j;
        }
        
        // call inside loopJ
        setTimeOut(j = loopJInside(j), 500);
        */var loopJ = function(valueI) {
        
            for (var j = valueI; j < (3+valueI); ) {
        	    
        	varJDeferred = $.Deferred();
                $("span").html($("span").html() + "<br>j is : " + j);
                j++;
                varJDeferred.resolve();
                
            }
            
            varIDeferred.resolve();
            
            return (valueI+1);
        };
        
       	for(var i = 0; i < 3; ) {
       	    
       	    varIDeferred = $.Deferred();
       	    
	    $("span").html($("span").html() + "<br>value of i is : " + i);
            
            if (i == 3)
                break;
            	
            i = loopJ(i);
        };
        
    });
});
<!DOCTYPE html><html><head><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script></head><body><pstyle="cursor:default;border:2px solid black;width:60px;padding:5px;">Click me</p><span><span></body></html>

Hope this would help many one.

Post a Comment for "Javascript: Wait For Function In Loop To Finish Executing Before Next Iteration"