jquery - Wait untill nested functions complete to continue next .each() -
how can make .each()
wait until nested functions completed?
heres code
html:
<div> <p>text 1</p> <p>text 2</p> <p>text 3</p> </div>
jquery:
function function1(){ $.get(geturl, function(data) { if(data == "false"){ postlog("error: not product number or description", "error");`//postlog adds new line on page log` }else if(data == "true"){ postlog("retrieved product number , description", "proccess"); postlog("completed crawl", "success"); } }); } function function2(){ //do stuff } $('p').each(function() { function1(); function2(); });
basically functions 1 & 2 text 1, once functions complete functions 1 & 2 text 2...
if has ideas great.
edit
sorry forgot mention few details..
in function 1 calls external php script , takes 30secs functions 1 return true. time function 2 has returned next each()
has been run.
make use of callbacks
.
function function1(control,callback) { console.log("executing function 1 "+control.text()); callback(control);//call 2nd function here } function function2(control) { console.log("executing function 2 "+control.text()); } $('p').each(function() { function1($(this),function2); //pass callback function });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <p>text 1</p> <p>text 2</p> <p>text 3</p> </div>
update
there wouldn't change move callback function
within get
below:
function function1(control,callback) { $.get(geturl, function(data) { if(data == "false"){ postlog("error: not product number or description", "error");`//postlog adds new line on page log` }else if(data == "true"){ postlog("retrieved product number , description", "proccess"); postlog("completed crawl", "success"); } callback(control); //call here now. }); }
Comments
Post a Comment